use of org.hibernate.stat.Statistics in project hibernate-orm by hibernate.
the class ReadWriteTest method testQueryCache.
@Test
public void testQueryCache() throws Exception {
Statistics stats = sessionFactory().getStatistics();
stats.clear();
Item item = new Item("chris", "Chris's Item");
withTxSession(s -> s.persist(item));
// Delay added to guarantee that query cache results won't be considered
// as not up to date due to persist session and query results from first
// query happening simultaneously.
TIME_SERVICE.advance(1);
withTxSession(s -> s.createQuery("from Item").setCacheable(true).list());
withTxSession(s -> {
s.createQuery("from Item").setCacheable(true).list();
assertEquals(1, stats.getQueryCacheHitCount());
s.createQuery("delete from Item").executeUpdate();
});
}
use of org.hibernate.stat.Statistics 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.Statistics 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.Statistics 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.Statistics in project hibernate-orm by hibernate.
the class NaturalIdOnSingleManyToOneTest method testManyToOneNaturalIdCached.
@Test
public void testManyToOneNaturalIdCached() {
NaturalIdOnManyToOne singleManyToOne = new NaturalIdOnManyToOne();
Citizen c1 = new Citizen();
c1.setFirstname("Emmanuel");
c1.setLastname("Bernard");
c1.setSsn("1234");
State france = new State();
france.setName("Ile de France");
c1.setState(france);
singleManyToOne.setCitizen(c1);
Session s = openSession();
Transaction tx = s.beginTransaction();
s.persist(france);
s.persist(c1);
s.persist(singleManyToOne);
tx.commit();
s.close();
s.getSessionFactory().getCache().evictNaturalIdRegions();
Statistics stats = sessionFactory().getStatistics();
stats.setStatisticsEnabled(true);
stats.clear();
assertEquals("NaturalId cache puts should be zero", 0, stats.getNaturalIdCachePutCount());
assertEquals("NaturalId cache hits should be zero", 0, stats.getNaturalIdCacheHitCount());
assertEquals("NaturalId Cache Puts", 0, stats.getNaturalIdCachePutCount());
assertEquals("NaturalId cache misses should be zero", 0, stats.getNaturalIdCacheMissCount());
s = openSession();
tx = s.beginTransaction();
Criteria criteria = s.createCriteria(NaturalIdOnManyToOne.class);
criteria.add(Restrictions.naturalId().set("citizen", c1));
criteria.setCacheable(true);
// first query
List results = criteria.list();
assertEquals(1, results.size());
assertEquals("NaturalId Cache Hits", 0, stats.getNaturalIdCacheHitCount());
assertEquals("NaturalId Cache Misses", 1, stats.getNaturalIdCacheMissCount());
// one for Citizen, one for NaturalIdOnManyToOne
assertEquals("NaturalId Cache Puts", 2, stats.getNaturalIdCachePutCount());
assertEquals("NaturalId Cache Queries", 1, stats.getNaturalIdQueryExecutionCount());
// query a second time - result should be in session cache
criteria.list();
assertEquals("NaturalId Cache Hits", 0, stats.getNaturalIdCacheHitCount());
assertEquals("NaturalId Cache Misses", 1, stats.getNaturalIdCacheMissCount());
assertEquals("NaturalId Cache Puts", 2, stats.getNaturalIdCachePutCount());
assertEquals("NaturalId Cache Queries", 1, stats.getNaturalIdQueryExecutionCount());
// cleanup
tx.rollback();
s.close();
}
Aggregations