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();
SecondLevelCacheStatistics emp2LCStats = stats.getSecondLevelCacheStatistics(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";
}
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";
}
use of org.hibernate.stat.Statistics in project wildfly by wildfly.
the class SFSB2LC method addEntitiesAndEvictAll.
/**
* Insert 2 entities and put them into the 2LC and then evicts entity cache.
*/
public String addEntitiesAndEvictAll(String CACHE_REGION_NAME) {
EntityManager em = emf.createEntityManager();
Statistics stats = em.unwrap(Session.class).getSessionFactory().getStatistics();
stats.clear();
SecondLevelCacheStatistics emp2LCStats = stats.getSecondLevelCacheStatistics(CACHE_REGION_NAME + "Employee");
try {
createEmployee(em, "Jan", "Ostrava", 20);
createEmployee(em, "Martin", "Brno", 30);
assertEquals("There are 2 puts in the 2LC" + generateEntityCacheStats(emp2LCStats), 2, emp2LCStats.getPutCount());
assertTrue("Expected entities stored in the cache" + generateEntityCacheStats(emp2LCStats), emp2LCStats.getElementCountInMemory() > 0);
// evict entity 2lc
emf.getCache().evictAll();
} catch (AssertionError e) {
return e.getMessage();
} finally {
em.close();
}
return "OK";
}
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";
}
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();
SecondLevelCacheStatistics emp2LCStats = stats.getSecondLevelCacheStatistics(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";
}
Aggregations