use of org.hibernate.stat.SecondLevelCacheStatistics 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.SecondLevelCacheStatistics 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.SecondLevelCacheStatistics 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";
}
use of org.hibernate.stat.SecondLevelCacheStatistics in project wildfly by wildfly.
the class StatefulBean method logStats.
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());
}
// we will want to return the SecondLevelCacheStatistics for Employee
}
use of org.hibernate.stat.SecondLevelCacheStatistics in project gocd by gocd.
the class UserSqlMapDaoCachingTest method shouldCacheUserOnFind.
@Test
public void shouldCacheUserOnFind() {
User first = new User("first");
first.addNotificationFilter(new NotificationFilter("pipline", "stage1", StageEvent.Fails, true));
first.addNotificationFilter(new NotificationFilter("pipline", "stage2", StageEvent.Fails, true));
int originalUserCacheSize = sessionFactory.getStatistics().getSecondLevelCacheStatistics(User.class.getCanonicalName()).getEntries().size();
int originalNotificationsCacheSize = sessionFactory.getStatistics().getSecondLevelCacheStatistics(User.class.getCanonicalName() + ".notificationFilters").getEntries().size();
userDao.saveOrUpdate(first);
long userId = userDao.findUser("first").getId();
assertThat(sessionFactory.getStatistics().getSecondLevelCacheStatistics(User.class.getCanonicalName()).getEntries().size(), is(originalUserCacheSize + 1));
SecondLevelCacheStatistics notificationFilterCollectionCache = sessionFactory.getStatistics().getSecondLevelCacheStatistics(User.class.getCanonicalName() + ".notificationFilters");
assertThat(notificationFilterCollectionCache.getEntries().size(), is(originalNotificationsCacheSize + 1));
assertThat(notificationFilterCollectionCache.getEntries().get(userId), is(Matchers.notNullValue()));
}
Aggregations