Search in sources :

Example 51 with Statistics

use of org.hibernate.stat.Statistics in project wildfly by wildfly.

the class SFSB2LC method queryCacheCheckIfEmpty.

/**
 * Checking if query cache is empty
 *
 * @param id Employee's id in the query
 */
public String queryCacheCheckIfEmpty(String id) {
    EntityManager em = emf.createEntityManager();
    Statistics stats = em.unwrap(Session.class).getSessionFactory().getStatistics();
    stats.clear();
    try {
        // the nextTimestamp from infinispan is "return System.currentTimeMillis() / 100;"
        Thread.sleep(1000);
        String queryString = "from Employee e where e.id > " + id;
        QueryStatistics queryStats = stats.getQueryStatistics(queryString);
        Query query = em.createQuery(queryString);
        query.setHint("org.hibernate.cacheable", true);
        // query - this call shouldn't hit the cache -> query cache is empty
        query.getResultList();
        assertEquals("Expected 1 miss in cache" + generateQueryCacheStats(queryStats), 1, queryStats.getCacheMissCount());
        assertEquals("Expected 1 put in cache" + generateQueryCacheStats(queryStats), 1, queryStats.getCachePutCount());
        assertEquals("Expected no hits in cache" + generateQueryCacheStats(queryStats), 0, queryStats.getCacheHitCount());
    } catch (AssertionError e) {
        return e.getMessage();
    } catch (InterruptedException e) {
        return e.getMessage();
    } finally {
        em.close();
    }
    return "OK";
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) QueryStatistics(org.hibernate.stat.QueryStatistics) QueryStatistics(org.hibernate.stat.QueryStatistics) CacheRegionStatistics(org.hibernate.stat.CacheRegionStatistics) Statistics(org.hibernate.stat.Statistics)

Example 52 with Statistics

use of org.hibernate.stat.Statistics in project wildfly by wildfly.

the class SFSB2LC method queryCacheCheck.

/**
 * Performs 2 query calls, first call put query in the cache and second should hit the cache
 *
 * @param id Employee's id in the query
 */
public String queryCacheCheck(String id) {
    // the nextTimestamp from infinispan is "return System.currentTimeMillis()"
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
        return e.getMessage();
    }
    EntityManager em = emf.createEntityManager();
    Statistics stats = em.unwrap(Session.class).getSessionFactory().getStatistics();
    stats.clear();
    try {
        String queryString = "from Employee e where e.id > " + id;
        QueryStatistics queryStats = stats.getQueryStatistics(queryString);
        Query query = em.createQuery(queryString);
        query.setHint("org.hibernate.cacheable", true);
        // query - this call should fill the cache
        query.getResultList();
        assertEquals("Expected 1 miss in cache" + generateQueryCacheStats(queryStats), 1, queryStats.getCacheMissCount());
        assertEquals("Expected 1 put in cache" + generateQueryCacheStats(queryStats), 1, queryStats.getCachePutCount());
        assertEquals("Expected no hits in cache" + generateQueryCacheStats(queryStats), 0, queryStats.getCacheHitCount());
        // query - second call should hit cache
        query.getResultList();
        assertEquals("Expected 1 hit in cache" + generateQueryCacheStats(queryStats), 1, queryStats.getCacheHitCount());
    } catch (AssertionError e) {
        return e.getMessage();
    } finally {
        em.close();
    }
    return "OK";
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) QueryStatistics(org.hibernate.stat.QueryStatistics) QueryStatistics(org.hibernate.stat.QueryStatistics) CacheRegionStatistics(org.hibernate.stat.CacheRegionStatistics) Statistics(org.hibernate.stat.Statistics)

Example 53 with Statistics

use of org.hibernate.stat.Statistics in project wildfly by wildfly.

the class SFSB2LC method secondSessionCheck.

/**
 * Checking entity 2LC in a different EntityManager session
 */
public String secondSessionCheck(String CACHE_REGION_NAME) {
    EntityManager em = emf.createEntityManager();
    Statistics stats = em.unwrap(Session.class).getSessionFactory().getStatistics();
    stats.clear();
    CacheRegionStatistics emp2LCStats = stats.getDomainDataRegionStatistics(CACHE_REGION_NAME + "Employee");
    try {
        // add new entity
        createEmployee(em, "David", "Praha", 10);
        assertEquals("There is 1 put in the 2LC" + generateEntityCacheStats(emp2LCStats), 1, emp2LCStats.getPutCount());
    } catch (AssertionError e) {
        return e.getMessage();
    } finally {
        em.close();
    }
    EntityManager em2 = emf.createEntityManager();
    try {
        // loading entity stored in previous session, we'are expecting hit in cache
        Employee emp = getEmployee(em2, 10);
        assertNotNull("Employee returned", emp);
        assertEquals("Expected 1 hit in 2LC" + generateEntityCacheStats(emp2LCStats), 1, emp2LCStats.getHitCount());
    } catch (AssertionError e) {
        return e.getMessage();
    } finally {
        em2.close();
    }
    return "OK";
}
Also used : EntityManager(javax.persistence.EntityManager) QueryStatistics(org.hibernate.stat.QueryStatistics) CacheRegionStatistics(org.hibernate.stat.CacheRegionStatistics) Statistics(org.hibernate.stat.Statistics) CacheRegionStatistics(org.hibernate.stat.CacheRegionStatistics)

Example 54 with Statistics

use of org.hibernate.stat.Statistics in project wildfly by wildfly.

the class SFSB2LC method evictedEntityCacheCheck.

/**
 * Checks if entity 2LC is empty.
 */
public String evictedEntityCacheCheck(String CACHE_REGION_NAME) {
    EntityManager em = emf.createEntityManager();
    Statistics stats = em.unwrap(Session.class).getSessionFactory().getStatistics();
    stats.clear();
    CacheRegionStatistics emp2LCStats = stats.getDomainDataRegionStatistics(CACHE_REGION_NAME + "Employee");
    try {
        assertEquals("Expected no entities stored in the cache" + emp2LCStats, 0, emp2LCStats.getElementCountInMemory());
        // loading entity stored in previous session, we are expecting miss in 2lc
        Employee emp = getEmployee(em, 20);
        assertNotNull("Employee returned", emp);
        assertEquals("Expected 1 miss in 2LC" + generateEntityCacheStats(emp2LCStats), 1, emp2LCStats.getMissCount());
    } catch (AssertionError e) {
        return e.getMessage();
    } finally {
        em.close();
    }
    return "OK";
}
Also used : EntityManager(javax.persistence.EntityManager) QueryStatistics(org.hibernate.stat.QueryStatistics) CacheRegionStatistics(org.hibernate.stat.CacheRegionStatistics) Statistics(org.hibernate.stat.Statistics) CacheRegionStatistics(org.hibernate.stat.CacheRegionStatistics)

Example 55 with Statistics

use of org.hibernate.stat.Statistics in project wildfly by wildfly.

the class SFSB4LC method entityCacheCheck.

/**
 * Performs 2 query calls, first call put entity in the cache and second should hit the cache
 *
 * @param id Employee's id in the query
 */
public String entityCacheCheck(int id) {
    // the nextTimestamp from infinispan is "return System.currentTimeMillis()"
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
        return e.getMessage();
    }
    EntityManager em = emf.createEntityManager();
    Statistics stats = em.unwrap(Session.class).getSessionFactory().getStatistics();
    stats.clear();
    try {
        String queryString = "from Employee e where e.id = " + id;
        QueryStatistics queryStats = stats.getQueryStatistics(queryString);
        Query query = em.createQuery(queryString);
        query.setHint("org.hibernate.cacheable", true);
        // query - this call should fill the cache
        query.getResultList();
        assertEquals("Expected 1 miss in cache" + generateQueryCacheStats(queryStats), 1, queryStats.getCacheMissCount());
        assertEquals("Expected 1 put in cache" + generateQueryCacheStats(queryStats), 1, queryStats.getCachePutCount());
        assertEquals("Expected no hits in cache" + generateQueryCacheStats(queryStats), 0, queryStats.getCacheHitCount());
        // query - second call should hit cache
        query.getResultList();
        assertEquals("Expected 1 hit in cache" + generateQueryCacheStats(queryStats), 1, queryStats.getCacheHitCount());
    } catch (AssertionError e) {
        return e.getMessage();
    } finally {
        em.close();
    }
    return "OK";
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) QueryStatistics(org.hibernate.stat.QueryStatistics) QueryStatistics(org.hibernate.stat.QueryStatistics) Statistics(org.hibernate.stat.Statistics)

Aggregations

Statistics (org.hibernate.stat.Statistics)97 Test (org.junit.Test)59 Session (org.hibernate.Session)27 SecondLevelCacheStatistics (org.hibernate.stat.SecondLevelCacheStatistics)26 EntityManager (javax.persistence.EntityManager)21 QueryStatistics (org.hibernate.stat.QueryStatistics)19 Item (org.hibernate.test.cache.infinispan.functional.entities.Item)14 Transaction (org.hibernate.Transaction)12 ArrayList (java.util.ArrayList)11 VersionedItem (org.hibernate.test.cache.infinispan.functional.entities.VersionedItem)11 List (java.util.List)10 OtherItem (org.hibernate.test.cache.infinispan.functional.entities.OtherItem)10 SessionFactory (org.hibernate.SessionFactory)9 CacheRegionStatistics (org.hibernate.stat.CacheRegionStatistics)9 ByRef (org.infinispan.commons.util.ByRef)8 TestForIssue (org.hibernate.testing.TestForIssue)6 Date (java.util.Date)5 UserTransaction (javax.transaction.UserTransaction)5 NaturalIdCacheStatistics (org.hibernate.stat.NaturalIdCacheStatistics)5 Test (org.testng.annotations.Test)5