Example usage for org.hibernate Session getSessionFactory

List of usage examples for org.hibernate Session getSessionFactory

Introduction

In this page you can find the example usage for org.hibernate Session getSessionFactory.

Prototype

SessionFactory getSessionFactory();

Source Link

Document

Get the session factory which created this session.

Usage

From source file:org.jboss.additional.testsuite.jdkall.present.clustering.cluster.ejb.xpc.bean.StatefulBean.java

License:Open Source License

@Override
public long getEmployeesInMemory() {
    Session session = em.unwrap(Session.class);
    String[] entityRegionNames = session.getSessionFactory().getStatistics().getSecondLevelCacheRegionNames();
    for (String name : entityRegionNames) {
        if (name.contains(Employee.class.getName())) {
            SecondLevelCacheStatistics stats = session.getSessionFactory().getStatistics()
                    .getSecondLevelCacheStatistics(name);
            return stats.getElementCountInMemory();
        }/* ww  w .  jav a  2 s .c  om*/
    }
    return -1;

}

From source file:org.jboss.additional.testsuite.jdkall.present.clustering.cluster.ejb.xpc.bean.StatefulBean.java

License:Open Source License

private void logStats(String methodName) {
    Session session = em.unwrap(Session.class);
    log.trace(methodName + "(version=" + version + ", HashMap version=" + valueBag.get("version")
            + ") logging statistics for session = " + session);
    session.getSessionFactory().getStatistics().setStatisticsEnabled(true);
    session.getSessionFactory().getStatistics().logSummary();
    String[] entityRegionNames = session.getSessionFactory().getStatistics().getSecondLevelCacheRegionNames();
    for (String name : entityRegionNames) {
        log.trace("cache entity region name = " + name);
        SecondLevelCacheStatistics stats = session.getSessionFactory().getStatistics()
                .getSecondLevelCacheStatistics(name);
        log.trace("2lc for " + name + ": " + stats.toString());

    }//w  w  w .j  av a 2  s.c om
    // we will want to return the SecondLevelCacheStatistics for Employee

}

From source file:org.jboss.as.test.clustering.cluster.ejb.xpc.bean.StatefulBean.java

License:Open Source License

private void logStats(String methodName) {
    Session session = em.unwrap(Session.class);
    log.info(methodName + "(version=" + version + ", HashMap version=" + valueBag.get("version")
            + ") logging statistics for session = " + session);
    session.getSessionFactory().getStatistics().setStatisticsEnabled(true);
    session.getSessionFactory().getStatistics().logSummary();
    String[] entityRegionNames = session.getSessionFactory().getStatistics().getSecondLevelCacheRegionNames();
    for (String name : entityRegionNames) {
        log.info("cache entity region name = " + name);
        SecondLevelCacheStatistics stats = session.getSessionFactory().getStatistics()
                .getSecondLevelCacheStatistics(name);
        log.info("2lc for " + name + ": " + stats.toString());

    }//from  w  w  w . ja  v a2  s .com
    // we will want to return the SecondLevelCacheStatistics for Employee

}

From source file:org.jboss.as.test.clustering.cluster.ejb3.xpc.bean.StatefulBean.java

License:Open Source License

@Override
public long getEmployeesInMemory() {
    Session session = em.unwrap(Session.class);
    String entityRegionNames[] = session.getSessionFactory().getStatistics().getSecondLevelCacheRegionNames();
    for (String name : entityRegionNames) {
        if (name.contains(Employee.class.getName())) {
            SecondLevelCacheStatistics stats = session.getSessionFactory().getStatistics()
                    .getSecondLevelCacheStatistics(name);
            return stats.getElementCountInMemory();
        }//from   ww w.j  a va  2 s. c  o  m
    }
    return -1;

}

From source file:org.jboss.as.test.clustering.cluster.ejb3.xpc.bean.StatefulBean.java

License:Open Source License

private void logStats(String methodName) {
    Session session = em.unwrap(Session.class);
    System.out.println(methodName + "(version=" + version + ", HashMap version=" + valueBag.get("version")
            + ") logging statistics for session = " + session);
    session.getSessionFactory().getStatistics().setStatisticsEnabled(true);
    session.getSessionFactory().getStatistics().logSummary();
    String entityRegionNames[] = session.getSessionFactory().getStatistics().getSecondLevelCacheRegionNames();
    for (String name : entityRegionNames) {
        System.out.println("cache entity region name = " + name);
        SecondLevelCacheStatistics stats = session.getSessionFactory().getStatistics()
                .getSecondLevelCacheStatistics(name);
        System.out.println("2lc for " + name + ": " + stats.toString());

    }//from   w  w  w . java  2s  . c o m
    // we will want to return the SecondLevelCacheStatistics for Employee

}

From source file:org.jboss.capedwarf.server.jee.persistence.HibernateStatelessAdapterFactory.java

License:Open Source License

protected StatelessAdapter doCreateStatelessAdapter(EntityManager em) {
    Object delegate = em.getDelegate();
    if (delegate instanceof Session == false)
        throw new IllegalArgumentException("Can only handle Hibernate Session: " + delegate);

    Session session = (Session) delegate;
    SessionFactory factory = session.getSessionFactory();
    return new HibernateStatelessAdapter(factory.openStatelessSession());
}

From source file:org.jboss.dashboard.database.hibernate.HibernateInitializer.java

License:Apache License

protected void verifyHibernateConfig() throws Exception {
    new HibernateTxFragment() {
        protected void txFragment(Session session) throws Exception {
            Map metadata = session.getSessionFactory().getAllClassMetadata();
            for (Iterator i = metadata.values().iterator(); i.hasNext();) {
                final AbstractEntityPersister persister = (AbstractEntityPersister) i.next();
                final String className = persister.getName();
                if (!ArrayUtils.contains(verificationExcludedClassNames, className)) {
                    log.debug("Verifying: " + className);
                    new HibernateTxFragment(true) {
                        protected void txFragment(Session session) throws Exception {
                            try {
                                boolean usingOracle = isOracleDatabase();
                                Query query = session.createQuery(
                                        "from " + className + " c " + (usingOracle ? " where rownum < 6" : ""));
                                if (!usingOracle)
                                    query.setMaxResults(5);
                                query.list();
                            } catch (Exception e) {
                                log.error("Structure verification error for class " + className);
                                log.error("Error seems to affect table named " + persister.getTableName());
                                log.error(
                                        "The following stack trace may help you to determine the current error cause: ",
                                        e);
                            }// www  .j  av a  2 s. com
                        }
                    }.execute();
                }
            }
        }
    }.execute();
}

From source file:org.jboss.pnc.datastore.repositories.DefaultSequenceHandlerRepository.java

License:Open Source License

@Override
public Long getNextID(final String sequenceName) {

    ReturningWork<Long> maxReturningWork = new ReturningWork<Long>() {
        @Override//from   www .  j a v a2  s .c  o  m
        public Long execute(Connection connection) throws SQLException {
            DialectResolver dialectResolver = new StandardDialectResolver();
            Dialect dialect = dialectResolver.resolveDialect(getResolutionInfo(connection));
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
            try {
                preparedStatement = connection.prepareStatement(dialect.getSequenceNextValString(sequenceName));
                resultSet = preparedStatement.executeQuery();
                resultSet.next();
                return resultSet.getLong(1);
            } catch (SQLException e) {
                throw e;
            } finally {
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (resultSet != null) {
                    resultSet.close();
                }
            }

        }
    };

    Session session = (Session) entityManager.getDelegate();
    SessionFactory sessionFactory = session.getSessionFactory();

    Long maxRecord = sessionFactory.getCurrentSession().doReturningWork(maxReturningWork);
    return maxRecord;
}

From source file:org.jboss.pnc.datastore.repositories.DefaultSequenceHandlerRepository.java

License:Open Source License

@Override
public void createSequence(final String sequenceName) {

    if (sequenceExists(sequenceName)) {
        return;//  ww  w .ja  v  a2 s . c o m
    }
    Work work = new Work() {
        @Override
        public void execute(Connection connection) throws SQLException {
            DialectResolver dialectResolver = new StandardDialectResolver();
            Dialect dialect = dialectResolver.resolveDialect(getResolutionInfo(connection));
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
            try {
                preparedStatement = connection
                        .prepareStatement(dialect.getCreateSequenceStrings(sequenceName, 1, 1)[0]);
                preparedStatement.execute();
            } catch (SQLException e) {
                throw e;
            } finally {
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (resultSet != null) {
                    resultSet.close();
                }
            }

        }
    };

    Session session = (Session) entityManager.getDelegate();
    SessionFactory sessionFactory = session.getSessionFactory();
    sessionFactory.getCurrentSession().doWork(work);
}

From source file:org.jboss.pnc.datastore.repositories.DefaultSequenceHandlerRepository.java

License:Open Source License

@Override
public boolean sequenceExists(final String sequenceName) {
    ReturningWork<Boolean> work = new ReturningWork<Boolean>() {
        @Override//from w w w.  j  a  v a  2  s . c om
        public Boolean execute(Connection connection) throws SQLException {
            DialectResolver dialectResolver = new StandardDialectResolver();
            Dialect dialect = dialectResolver.resolveDialect(getResolutionInfo(connection));
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
            try {
                preparedStatement = connection.prepareStatement(dialect.getQuerySequencesString());
                resultSet = preparedStatement.executeQuery();
                while (resultSet.next()) {
                    if (sequenceName.equals(resultSet.getString(1))) {
                        return true;
                    }
                }
            } catch (SQLException e) {
                throw e;
            } finally {
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (resultSet != null) {
                    resultSet.close();
                }
            }
            return false;

        }
    };

    Session session = (Session) entityManager.getDelegate();
    SessionFactory sessionFactory = session.getSessionFactory();
    return sessionFactory.getCurrentSession().doReturningWork(work);
}