Search in sources :

Example 6 with HibernateEntityManagerFactory

use of org.hibernate.jpa.HibernateEntityManagerFactory in project wildfly by wildfly.

the class HibernateQueryCacheStatistics method getBaseStatistics.

private org.hibernate.stat.Statistics getBaseStatistics(EntityManagerFactory entityManagerFactory) {
    if (entityManagerFactory == null) {
        return null;
    }
    HibernateEntityManagerFactory entityManagerFactoryImpl = (HibernateEntityManagerFactory) entityManagerFactory;
    SessionFactory sessionFactory = entityManagerFactoryImpl.getSessionFactory();
    if (sessionFactory != null) {
        return sessionFactory.getStatistics();
    }
    return null;
}
Also used : SessionFactory(org.hibernate.SessionFactory) HibernateEntityManagerFactory(org.hibernate.jpa.HibernateEntityManagerFactory)

Example 7 with HibernateEntityManagerFactory

use of org.hibernate.jpa.HibernateEntityManagerFactory in project wildfly by wildfly.

the class HibernateStatistics method getStatistics.

static final org.hibernate.stat.Statistics getStatistics(final EntityManagerFactory entityManagerFactory) {
    if (entityManagerFactory == null) {
        return null;
    }
    HibernateEntityManagerFactory entityManagerFactoryImpl = (HibernateEntityManagerFactory) entityManagerFactory;
    SessionFactory sessionFactory = entityManagerFactoryImpl.getSessionFactory();
    if (sessionFactory != null) {
        return sessionFactory.getStatistics();
    }
    return null;
}
Also used : SessionFactory(org.hibernate.SessionFactory) HibernateEntityManagerFactory(org.hibernate.jpa.HibernateEntityManagerFactory)

Example 8 with HibernateEntityManagerFactory

use of org.hibernate.jpa.HibernateEntityManagerFactory in project hibernate-orm by hibernate.

the class EntityManagerFactoryUnwrapTest method testEntityManagerCanBeUnwrappedToDeprecatedHibernateEntityManagerFactory.

@Test
public void testEntityManagerCanBeUnwrappedToDeprecatedHibernateEntityManagerFactory() {
    HibernateEntityManagerFactory hibernateEntityManagerFactory = entityManagerFactory.unwrap(HibernateEntityManagerFactory.class);
    assertNotNull("Unwrapping to SPI class HibernateEntityManagerFactory should be ok", hibernateEntityManagerFactory);
}
Also used : HibernateEntityManagerFactory(org.hibernate.jpa.HibernateEntityManagerFactory) Test(org.junit.Test)

Example 9 with HibernateEntityManagerFactory

use of org.hibernate.jpa.HibernateEntityManagerFactory in project hibernate-orm by hibernate.

the class CachedQueryTest method testCacheableQuery.

@Test
public void testCacheableQuery() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    for (int i = 0; i < 10; i++) {
        Employee employee = new Employee("John" + i, 20d + i);
        em.persist(employee);
    }
    em.getTransaction().commit();
    em.close();
    HibernateEntityManagerFactory hemf = (HibernateEntityManagerFactory) entityManagerFactory();
    Statistics stats = hemf.getSessionFactory().getStatistics();
    assertEquals(0, stats.getQueryCacheHitCount());
    assertEquals(0, stats.getQueryCacheMissCount());
    assertEquals(0, stats.getQueryCachePutCount());
    assertEquals(0, stats.getSecondLevelCacheHitCount());
    assertEquals(0, stats.getSecondLevelCacheMissCount());
    assertEquals(10, stats.getSecondLevelCachePutCount());
    stats.clear();
    em = getOrCreateEntityManager();
    em.getTransaction().begin();
    // First time the query is executed, query and results are cached.
    TypedQuery<Employee> query = em.createQuery("select e from Employee e", Employee.class).setHint(QueryHints.HINT_CACHEABLE, true);
    List<Employee> employees = query.getResultList();
    assertEquals(10, employees.size());
    assertEquals(0, stats.getQueryCacheHitCount());
    assertEquals(1, stats.getQueryCacheMissCount());
    assertEquals(1, stats.getQueryCachePutCount());
    // the first time the query executes, stats.getSecondLevelCacheHitCount() is 0 because the
    // entities are read from the query ResultSet (not from the entity cache).
    assertEquals(0, stats.getSecondLevelCacheHitCount());
    assertEquals(0, stats.getSecondLevelCacheMissCount());
    assertEquals(0, stats.getSecondLevelCachePutCount());
    em.getTransaction().commit();
    em.close();
    stats.clear();
    // Second time the query is executed, list of entities are read from query cache and
    // the entities themselves are read from the entity cache.
    em = getOrCreateEntityManager();
    em.getTransaction().begin();
    query = em.createQuery("select e from Employee e", Employee.class).setHint(QueryHints.HINT_CACHEABLE, true);
    employees = query.getResultList();
    assertEquals(10, employees.size());
    assertEquals(1, stats.getQueryCacheHitCount());
    assertEquals(0, stats.getQueryCacheMissCount());
    assertEquals(0, stats.getQueryCachePutCount());
    // the first time the query executes, stats.getSecondLevelCacheHitCount() is 0 because the
    // entities are read from the query ResultSet (not from the entity cache).
    assertEquals(10, stats.getSecondLevelCacheHitCount());
    assertEquals(0, stats.getSecondLevelCacheMissCount());
    assertEquals(0, stats.getSecondLevelCachePutCount());
    em.getTransaction().commit();
    em.close();
    // NOTE: JPACache.evictAll() only evicts entity regions;
    //       it does not evict the collection regions or query cache region
    entityManagerFactory().getCache().evictAll();
    stats.clear();
    em = getOrCreateEntityManager();
    em.getTransaction().begin();
    query = em.createQuery("select e from Employee e", Employee.class).setHint(QueryHints.HINT_CACHEABLE, true);
    employees = query.getResultList();
    assertEquals(10, employees.size());
    // query is still found in the cache
    assertEquals(1, stats.getQueryCacheHitCount());
    assertEquals(0, stats.getQueryCacheMissCount());
    assertEquals(0, stats.getQueryCachePutCount());
    // since entity regions were evicted, the 10 entities are not found, and are re-put afterQuery loading
    // as each entity ID is read from the query cache, Hibernate will look the entity up in the
    // cache and will not find it; that's why the "miss" and "put" counts are both 10.
    assertEquals(0, stats.getSecondLevelCacheHitCount());
    assertEquals(10, stats.getSecondLevelCacheMissCount());
    assertEquals(10, stats.getSecondLevelCachePutCount());
    em.getTransaction().commit();
    em.close();
    stats.clear();
    // this time call clear the entity regions and the query cache region
    em = getOrCreateEntityManager();
    em.getEntityManagerFactory().getCache().evictAll();
    em.unwrap(HibernateEntityManagerImplementor.class).getFactory().getSessionFactory().getCache().evictQueryRegions();
    em.getTransaction().begin();
    query = em.createQuery("select e from Employee e", Employee.class).setHint(QueryHints.HINT_CACHEABLE, true);
    employees = query.getResultList();
    assertEquals(10, employees.size());
    // query is no longer found in the cache
    assertEquals(0, stats.getQueryCacheHitCount());
    assertEquals(1, stats.getQueryCacheMissCount());
    assertEquals(1, stats.getQueryCachePutCount());
    // stats.getSecondLevelCacheHitCount() is 0 because the
    // entities are read from the query ResultSet (not from the entity cache).
    assertEquals(0, stats.getSecondLevelCacheHitCount());
    assertEquals(0, stats.getSecondLevelCacheMissCount());
    assertEquals(10, stats.getSecondLevelCachePutCount());
    em.createQuery("delete from Employee").executeUpdate();
    em.getTransaction().commit();
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) HibernateEntityManagerFactory(org.hibernate.jpa.HibernateEntityManagerFactory) Statistics(org.hibernate.stat.Statistics) Test(org.junit.Test)

Example 10 with HibernateEntityManagerFactory

use of org.hibernate.jpa.HibernateEntityManagerFactory in project wildfly by wildfly.

the class HibernateEntityCacheStatistics method getStatistics.

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;
    }
    SessionFactory sessionFactory = entityManagerFactoryImpl.getSessionFactory();
    if (sessionFactory != null) {
        return sessionFactory.getStatistics().getSecondLevelCacheStatistics(scopedPersistenceUnitName + "." + pathAddress.getValue(HibernateStatistics.ENTITYCACHE));
    }
    return null;
}
Also used : SessionFactory(org.hibernate.SessionFactory) HibernateEntityManagerFactory(org.hibernate.jpa.HibernateEntityManagerFactory)

Aggregations

HibernateEntityManagerFactory (org.hibernate.jpa.HibernateEntityManagerFactory)13 SessionFactory (org.hibernate.SessionFactory)9 Test (org.junit.Test)4 HashMap (java.util.HashMap)1 Map (java.util.Map)1 EntityManager (javax.persistence.EntityManager)1 EntityManagerFactoryBuilder (org.hibernate.jpa.boot.spi.EntityManagerFactoryBuilder)1 BaseEntityManagerFunctionalTestCase (org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase)1 Statistics (org.hibernate.stat.Statistics)1 TestForIssue (org.hibernate.testing.TestForIssue)1