use of org.hibernate.stat.SecondLevelCacheStatistics in project hibernate-orm by hibernate.
the class ReadWriteTest method testStaleWritesLeaveCacheConsistent.
@Test
public void testStaleWritesLeaveCacheConsistent() throws Exception {
Statistics stats = sessionFactory().getStatistics();
stats.clear();
ByRef<VersionedItem> itemRef = new ByRef<>(null);
withTxSession(s -> {
VersionedItem item = new VersionedItem();
item.setName("steve");
item.setDescription("steve's item");
s.save(item);
itemRef.set(item);
});
final VersionedItem item = itemRef.get();
Long initialVersion = item.getVersion();
// manually revert the version property
item.setVersion(new Long(item.getVersion().longValue() - 1));
try {
withTxSession(s -> s.update(item));
fail("expected stale write to fail");
} catch (Exception e) {
log.debug("Rollback was expected", e);
}
// check the version value in the cache...
SecondLevelCacheStatistics slcs = stats.getSecondLevelCacheStatistics(VersionedItem.class.getName());
Object entry = slcs.getEntries().get(item.getId());
Long cachedVersionValue;
cachedVersionValue = (Long) ((CacheEntry) entry).getVersion();
assertEquals(initialVersion.longValue(), cachedVersionValue.longValue());
withTxSession(s -> {
VersionedItem item2 = s.load(VersionedItem.class, item.getId());
s.delete(item2);
});
}
use of org.hibernate.stat.SecondLevelCacheStatistics in project hibernate-orm by hibernate.
the class ReadWriteTest method testPersistEntityFlushEvictGetRollbackNotInEntityCache.
@Test
@TestForIssue(jiraKey = "HHH-5690")
public void testPersistEntityFlushEvictGetRollbackNotInEntityCache() throws Exception {
Statistics stats = sessionFactory().getStatistics();
stats.clear();
SecondLevelCacheStatistics slcs = stats.getSecondLevelCacheStatistics(Item.class.getName());
ByRef<Long> itemId = new ByRef<>(null);
withTxSession(s -> {
Item item = new Item();
item.setName("steve");
item.setDescription("steve's item");
s.persist(item);
s.flush();
itemId.set(item.getId());
s.evict(item);
assertEquals(slcs.getHitCount(), 0);
item = s.get(Item.class, item.getId());
assertNotNull(item);
markRollbackOnly(s);
});
// item should not be in entity cache.
//slcs = stats.getSecondLevelCacheStatistics( Item.class.getName() );
assertEquals(Collections.EMPTY_MAP, slcs.getEntries());
withTxSession(s -> {
Item item = s.get(Item.class, itemId.get());
assertNull(item);
});
}
use of org.hibernate.stat.SecondLevelCacheStatistics in project hibernate-orm by hibernate.
the class ReadWriteTest method testPersistEntityFlushRollbackNotInEntityCache.
@Test
@TestForIssue(jiraKey = "HHH-5690")
public void testPersistEntityFlushRollbackNotInEntityCache() throws Exception {
Statistics stats = sessionFactory().getStatistics();
stats.clear();
SecondLevelCacheStatistics slcs = stats.getSecondLevelCacheStatistics(Item.class.getName());
ByRef<Long> itemId = new ByRef<>(null);
withTxSession(s -> {
Item item = new Item();
item.setName("steve");
item.setDescription("steve's item");
s.persist(item);
s.flush();
itemId.set(item.getId());
markRollbackOnly(s);
});
// item should not be in entity cache.
assertEquals(Collections.EMPTY_MAP, slcs.getEntries());
withTxSession(s -> {
Item item = s.get(Item.class, itemId.get());
assertNull(item);
});
}
use of org.hibernate.stat.SecondLevelCacheStatistics in project wildfly by wildfly.
the class StatefulBean method getEmployeesInMemory.
@Override
public long getEmployeesInMemory() {
Session session = em.unwrap(Session.class);
String[] entityRegionNames = session.getSessionFactory().getStatistics().getSecondLevelCacheRegionNames();
for (String name : entityRegionNames) {
if (name.contains(Employee.class.getName())) {
SecondLevelCacheStatistics stats = session.getSessionFactory().getStatistics().getSecondLevelCacheStatistics(name);
return stats.getElementCountInMemory();
}
}
return -1;
}
use of org.hibernate.stat.SecondLevelCacheStatistics in project wildfly by wildfly.
the class SFSB2LC method sameSessionCheck.
/**
* Checking entity 2LC in one EntityManager session
*/
public String sameSessionCheck(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 entities and check if they are put in 2LC
createEmployee(em, "Peter", "Ostrava", 2);
createEmployee(em, "Tom", "Brno", 3);
assertEquals("There are 2 puts in the 2LC" + generateEntityCacheStats(emp2LCStats), 2, emp2LCStats.getPutCount());
// loading all Employee entities should put in 2LC all Employee
List<?> empList = getAllEmployeesQuery(em);
assertEquals("There are 2 entities.", empList.size(), 2);
assertEquals("There are 2 entities in the 2LC" + generateEntityCacheStats(emp2LCStats), 2, emp2LCStats.getElementCountInMemory());
// clear session
em.clear();
// entity should be loaded from 2L cache, we'are expecting hit in 2L cache
Employee emp = getEmployee(em, 2);
assertNotNull("Employee returned", emp);
assertEquals("Expected 1 hit in cache" + generateEntityCacheStats(emp2LCStats), 1, emp2LCStats.getHitCount());
} catch (AssertionError e) {
return e.getMessage();
} finally {
em.close();
}
return "OK";
}
Aggregations