Example usage for org.hibernate SessionFactory getStatistics

List of usage examples for org.hibernate SessionFactory getStatistics

Introduction

In this page you can find the example usage for org.hibernate SessionFactory getStatistics.

Prototype

Statistics getStatistics();

Source Link

Document

Retrieve the statistics fopr this factory.

Usage

From source file:org.gridgain.examples.jpa.JpaHibernateExample.java

License:Apache License

/**
 * Prints Hibernate L2 cache statistics to standard output.
 *
 * @param sesFactory Hibernate {@link SessionFactory}, for which to print
 *                   statistics./*www  .j a  va2  s  . co m*/
 */
private static void printStats(SessionFactory sesFactory) {
    System.out.println("=== Hibernate L2 cache statistics ===");

    for (String entityName : ENTITY_NAMES) {
        System.out.println("\tEntity: " + entityName);

        SecondLevelCacheStatistics stats = sesFactory.getStatistics().getSecondLevelCacheStatistics(entityName);

        System.out.println("\t\tHits: " + stats.getHitCount());
        System.out.println("\t\tMisses: " + stats.getMissCount());
    }

    System.out.println("=====================================");
}

From source file:org.horizontaldb.shard.hibernate.AbstractDaoEnricherTest.java

License:Apache License

@Test
public void shouldSetAndUnsetSessionForDao() {
    AbstractDaoEnricher enricher = new AbstractDaoEnricher(txManager);

    ShardContext shardContext = new ShardContext("testClient");
    SessionFactory mockSessionFactory = EasyMock.createMock(SessionFactory.class);
    Session mockSession = EasyMock.createMock("mockSession", Session.class);
    AbstractDao mockDao = EasyMock.createMock("mockDao", AbstractDao.class);

    // called during statistics logging
    expect(mockSession.getSessionFactory()).andReturn(mockSessionFactory);
    expect(mockSessionFactory.getStatistics()).andReturn(null);
    // called during statistics logging

    expect(txManager.getSessionFactory()).andReturn(mockSessionFactory);
    expect(mockSessionFactory.getCurrentSession()).andReturn(mockSession);
    // setup//www .  j a  v  a2 s  .c om
    mockDao.setSession(mockSession);
    // tearDown
    mockDao.setSession(null);

    replay(txManager, mockSessionFactory, mockSession, mockDao);

    enricher.setup(mockDao, shardContext);

    enricher.tearDown(mockDao, shardContext);

    verify(txManager, mockSessionFactory, mockSession, mockDao);
}

From source file:org.horizontaldb.shard.hibernate.AbstractDaoEnricherTest.java

License:Apache License

@Test
public void shouldSetAndUnsetDistinctSessionsForDaoUsingDistinctContexts() {
    AbstractDaoEnricher enricher = new AbstractDaoEnricher(txManager);

    ShardContext shardContext = new ShardContext("testClient");
    ShardContext shardContext1 = new ShardContext("testClient1");
    ShardContext shardContext2 = new ShardContext("testClient2");

    SessionFactory mockSessionFactory = EasyMock.createMock(SessionFactory.class);
    Session mockSession = EasyMock.createMock("mockSession", Session.class);
    Session mockSession1 = EasyMock.createMock("mockSession1", Session.class);
    Session mockSession2 = EasyMock.createMock("mockSession2", Session.class);
    AbstractDao mockDao = EasyMock.createMock("mockDao", AbstractDao.class);

    // called during statistics logging
    expect(mockSession2.getSessionFactory()).andReturn(mockSessionFactory);
    expect(mockSession1.getSessionFactory()).andReturn(mockSessionFactory);
    expect(mockSession.getSessionFactory()).andReturn(mockSessionFactory);
    expect(mockSessionFactory.getStatistics()).andReturn(null).times(3);
    // called during statistics logging

    expect(txManager.getSessionFactory()).andReturn(mockSessionFactory).times(3);
    expect(mockSessionFactory.getCurrentSession()).andReturn(mockSession);
    expect(mockSessionFactory.getCurrentSession()).andReturn(mockSession1);
    expect(mockSessionFactory.getCurrentSession()).andReturn(mockSession2);
    // setups//from www.ja v  a 2 s  .c  o  m
    mockDao.setSession(mockSession);
    mockDao.setSession(mockSession1);
    mockDao.setSession(mockSession2);
    // tearDowns
    mockDao.setSession(mockSession1);
    mockDao.setSession(mockSession);
    mockDao.setSession(null);

    replay(txManager, mockSessionFactory, mockSession, mockSession1, mockSession2, mockDao);

    enricher.setup(mockDao, shardContext);
    enricher.setup(mockDao, shardContext1);
    enricher.setup(mockDao, shardContext2);

    enricher.tearDown(mockDao, shardContext2);
    enricher.tearDown(mockDao, shardContext1);
    enricher.tearDown(mockDao, shardContext);

    verify(txManager, mockSessionFactory, mockSession, mockSession1, mockSession2, mockDao);
}

From source file:org.horizontaldb.shard.hibernate.AbstractDaoEnricherTest.java

License:Apache License

@Test
public void shouldSetAndUnsetProperSessionsForDistinctDaosUsingDistinctContexts() {
    AbstractDaoEnricher enricher = new AbstractDaoEnricher(txManager);

    ShardContext shardContext = new ShardContext("testClient");
    ShardContext shardContext1 = new ShardContext("testClient1");
    ShardContext shardContext2 = new ShardContext("testClient2");

    SessionFactory mockSessionFactory = EasyMock.createMock(SessionFactory.class);
    Session mockSession = EasyMock.createMock("mockSession", Session.class);
    Session mockSession1 = EasyMock.createMock("mockSession1", Session.class);
    Session mockSession2 = EasyMock.createMock("mockSession2", Session.class);
    AbstractDao mockDao = EasyMock.createMock("mockDao", AbstractDao.class);
    DepartmentDaoImpl mockDao1 = EasyMock.createMock("mockDao1", DepartmentDaoImpl.class);
    PersonDaoImpl mockDao2 = EasyMock.createMock("mockDao2", PersonDaoImpl.class);

    // called during statistics logging
    expect(mockSession2.getSessionFactory()).andReturn(mockSessionFactory);
    expect(mockSession1.getSessionFactory()).andReturn(mockSessionFactory);
    expect(mockSession.getSessionFactory()).andReturn(mockSessionFactory);
    expect(mockSessionFactory.getStatistics()).andReturn(null).times(3);
    // called during statistics logging

    expect(txManager.getSessionFactory()).andReturn(mockSessionFactory).times(3);
    expect(mockSessionFactory.getCurrentSession()).andReturn(mockSession);
    expect(mockSessionFactory.getCurrentSession()).andReturn(mockSession1);
    expect(mockSessionFactory.getCurrentSession()).andReturn(mockSession2);
    // setups/*ww w .j a  v  a  2 s  .  com*/
    mockDao.setSession(mockSession);
    mockDao1.setSession(mockSession);
    mockDao2.setSession(mockSession1);
    mockDao1.setSession(mockSession2);
    // tearDowns
    mockDao1.setSession(mockSession);
    mockDao2.setSession(null);
    mockDao1.setSession(null);
    mockDao.setSession(null);

    replay(txManager, mockSessionFactory, mockSession, mockSession1, mockSession2, mockDao, mockDao1, mockDao2);

    enricher.setup(mockDao, shardContext);
    enricher.setup(mockDao1, shardContext);
    enricher.setup(mockDao2, shardContext1);
    enricher.setup(mockDao1, shardContext2);

    enricher.tearDown(mockDao1, shardContext2);
    enricher.tearDown(mockDao2, shardContext1);
    enricher.tearDown(mockDao1, shardContext);
    enricher.tearDown(mockDao, shardContext);

    verify(txManager, mockSessionFactory, mockSession, mockSession1, mockSession2, mockDao, mockDao1, mockDao2);
}

From source file:org.jboss.as.jpa.hibernate4.management.HibernateCollectionStatistics.java

License:Open Source License

private org.hibernate.stat.Statistics getBaseStatistics(EntityManagerFactory entityManagerFactory) {
    if (entityManagerFactory == null) {
        return null;
    }/*from   w ww.  j a v a2s . c  o  m*/
    HibernateEntityManagerFactory entityManagerFactoryImpl = (HibernateEntityManagerFactory) entityManagerFactory;
    SessionFactory sessionFactory = entityManagerFactoryImpl.getSessionFactory();
    if (sessionFactory != null) {
        return sessionFactory.getStatistics();
    }
    return null;
}

From source file:org.jboss.as.jpa.hibernate4.management.HibernateCollectionStatistics.java

License:Open Source License

private CollectionStatistics getStatistics(final EntityManagerFactory entityManagerFactory,
        String collectionName) {//w w  w . ja  va  2 s. com
    if (entityManagerFactory == null) {
        return null;
    }
    HibernateEntityManagerFactory entityManagerFactoryImpl = (HibernateEntityManagerFactory) entityManagerFactory;
    SessionFactory sessionFactory = entityManagerFactoryImpl.getSessionFactory();
    if (sessionFactory != null) {
        return sessionFactory.getStatistics().getCollectionStatistics(collectionName);
    }
    return null;
}

From source file:org.jboss.as.jpa.hibernate4.management.HibernateEntityCacheStatistics.java

License:Open Source License

org.hibernate.stat.SecondLevelCacheStatistics getStatistics(
        EntityManagerFactoryAccess entityManagerFactoryaccess, PathAddress pathAddress) {
    String scopedPersistenceUnitName = pathAddress.getValue(HibernateStatistics.PROVIDER_LABEL);
    HibernateEntityManagerFactory entityManagerFactoryImpl = (HibernateEntityManagerFactory) entityManagerFactoryaccess
            .entityManagerFactory(scopedPersistenceUnitName);
    if (entityManagerFactoryImpl == null) {
        return null;
    }/*from   w w w.j  a  va2  s.co  m*/
    SessionFactory sessionFactory = entityManagerFactoryImpl.getSessionFactory();
    if (sessionFactory != null) {
        // The entity class name is prefixed by the application scoped persistence unit name

        return sessionFactory.getStatistics().getSecondLevelCacheStatistics(
                scopedPersistenceUnitName + "." + pathAddress.getValue(HibernateStatistics.ENTITYCACHE));
    }
    return null;
}

From source file:org.jboss.as.jpa.hibernate4.management.HibernateEntityStatistics.java

License:Open Source License

private org.hibernate.stat.EntityStatistics getStatistics(EntityManagerFactory entityManagerFactory,
        String entityName) {/*from  w ww .j a v  a  2 s  .co m*/
    if (entityManagerFactory == null) {
        return null;
    }
    HibernateEntityManagerFactory entityManagerFactoryImpl = (HibernateEntityManagerFactory) entityManagerFactory;
    SessionFactory sessionFactory = entityManagerFactoryImpl.getSessionFactory();
    if (sessionFactory != null) {
        return sessionFactory.getStatistics().getEntityStatistics(entityName);
    }
    return null;
}

From source file:org.jboss.as.jpa.hibernate4.management.HibernateQueryCacheStatistics.java

License:Open Source License

private org.hibernate.stat.QueryStatistics getStatistics(EntityManagerFactory entityManagerFactory,
        String displayQueryName) {
    if (entityManagerFactory == null) {
        return null;
    }/* w w  w .java 2  s  .  c o m*/
    HibernateEntityManagerFactory entityManagerFactoryImpl = (HibernateEntityManagerFactory) entityManagerFactory;
    SessionFactory sessionFactory = entityManagerFactoryImpl.getSessionFactory();
    // convert displayed (transformed by QueryNames) query name to original query name to look up query statistics
    if (sessionFactory != null) {
        String[] originalQueryNames = sessionFactory.getStatistics().getQueries();
        if (originalQueryNames != null) {
            for (String originalQueryName : originalQueryNames) {
                if (QueryName.queryName(originalQueryName).getDisplayName().equals(displayQueryName)) {
                    return sessionFactory.getStatistics().getQueryStatistics(originalQueryName);
                }
            }
        }
    }
    return null;
}

From source file:org.jboss.as.jpa.hibernate4.management.HibernateStatistics.java

License:Open Source License

static final org.hibernate.stat.Statistics getStatistics(final EntityManagerFactory entityManagerFactory) {
    if (entityManagerFactory == null) {
        return null;
    }//from   ww w.j ava 2s.c om
    HibernateEntityManagerFactory entityManagerFactoryImpl = (HibernateEntityManagerFactory) entityManagerFactory;
    SessionFactory sessionFactory = entityManagerFactoryImpl.getSessionFactory();
    if (sessionFactory != null) {
        return sessionFactory.getStatistics();
    }
    return null;
}