use of org.hibernate.stat.Statistics in project hibernate-orm by hibernate.
the class ReadWriteTest method testNaturalIdLoaderCached.
@Test
public void testNaturalIdLoaderCached() throws Exception {
final Statistics stats = sessionFactory().getStatistics();
stats.setStatisticsEnabled(true);
stats.clear();
assertEquals("NaturalId Cache Hits", 0, stats.getNaturalIdCacheHitCount());
assertEquals("NaturalId Cache Misses", 0, stats.getNaturalIdCacheMissCount());
assertEquals("NaturalId Cache Puts", 0, stats.getNaturalIdCachePutCount());
assertEquals("NaturalId Cache Queries", 0, stats.getNaturalIdQueryExecutionCount());
saveSomeCitizens();
assertEquals("NaturalId Cache Hits", 0, stats.getNaturalIdCacheHitCount());
assertEquals("NaturalId Cache Misses", 0, stats.getNaturalIdCacheMissCount());
assertEquals("NaturalId Cache Puts", 2, stats.getNaturalIdCachePutCount());
assertEquals("NaturalId Cache Queries", 0, stats.getNaturalIdQueryExecutionCount());
//Try NaturalIdLoadAccess after insert
final Citizen citizen = withTxSessionApply(s -> {
State france = ReadWriteTest.this.getState(s, "Ile de France");
NaturalIdLoadAccess<Citizen> naturalIdLoader = s.byNaturalId(Citizen.class);
naturalIdLoader.using("ssn", "1234").using("state", france);
stats.clear();
Citizen c = naturalIdLoader.load();
assertNotNull(c);
assertEquals("NaturalId Cache Hits", 1, stats.getNaturalIdCacheHitCount());
assertEquals("NaturalId Cache Misses", 0, stats.getNaturalIdCacheMissCount());
assertEquals("NaturalId Cache Puts", 0, stats.getNaturalIdCachePutCount());
assertEquals("NaturalId Cache Queries", 0, stats.getNaturalIdQueryExecutionCount());
markRollbackOnly(s);
return c;
});
// TODO: Clear caches manually via cache manager (it's faster!!)
cleanupCache();
TIME_SERVICE.advance(1);
stats.setStatisticsEnabled(true);
stats.clear();
//Try NaturalIdLoadAccess
withTxSession(s -> {
Citizen loadedCitizen = (Citizen) s.get(Citizen.class, citizen.getId());
assertNotNull(loadedCitizen);
assertEquals("NaturalId Cache Hits", 0, stats.getNaturalIdCacheHitCount());
assertEquals("NaturalId Cache Misses", 0, stats.getNaturalIdCacheMissCount());
assertEquals("NaturalId Cache Puts", 1, stats.getNaturalIdCachePutCount());
assertEquals("NaturalId Cache Queries", 0, stats.getNaturalIdQueryExecutionCount());
markRollbackOnly(s);
});
// Try NaturalIdLoadAccess after load
withTxSession(s -> {
State france = ReadWriteTest.this.getState(s, "Ile de France");
NaturalIdLoadAccess naturalIdLoader = s.byNaturalId(Citizen.class);
naturalIdLoader.using("ssn", "1234").using("state", france);
stats.setStatisticsEnabled(true);
stats.clear();
Citizen loadedCitizen = (Citizen) naturalIdLoader.load();
assertNotNull(loadedCitizen);
assertEquals("NaturalId Cache Hits", 1, stats.getNaturalIdCacheHitCount());
assertEquals("NaturalId Cache Misses", 0, stats.getNaturalIdCacheMissCount());
assertEquals("NaturalId Cache Puts", 0, stats.getNaturalIdCachePutCount());
assertEquals("NaturalId Cache Queries", 0, stats.getNaturalIdQueryExecutionCount());
markRollbackOnly(s);
});
}
use of org.hibernate.stat.Statistics in project hibernate-orm by hibernate.
the class ReadWriteTest method testEntityCacheContentsAfterEvictAll.
@Test
public void testEntityCacheContentsAfterEvictAll() throws Exception {
final List<Citizen> citizens = saveSomeCitizens();
withTxSession(s -> {
Cache cache = s.getSessionFactory().getCache();
Statistics stats = sessionFactory().getStatistics();
SecondLevelCacheStatistics slcStats = stats.getSecondLevelCacheStatistics(Citizen.class.getName());
assertTrue("2lc entity cache is expected to contain Citizen id = " + citizens.get(0).getId(), cache.containsEntity(Citizen.class, citizens.get(0).getId()));
assertTrue("2lc entity cache is expected to contain Citizen id = " + citizens.get(1).getId(), cache.containsEntity(Citizen.class, citizens.get(1).getId()));
assertEquals(2, slcStats.getPutCount());
cache.evictEntityRegions();
TIME_SERVICE.advance(1);
assertEquals(0, slcStats.getElementCountInMemory());
assertFalse("2lc entity cache is expected to not contain Citizen id = " + citizens.get(0).getId(), cache.containsEntity(Citizen.class, citizens.get(0).getId()));
assertFalse("2lc entity cache is expected to not contain Citizen id = " + citizens.get(1).getId(), cache.containsEntity(Citizen.class, citizens.get(1).getId()));
Citizen citizen = s.load(Citizen.class, citizens.get(0).getId());
assertNotNull(citizen);
assertNotNull(citizen.getFirstname());
assertEquals(1, slcStats.getMissCount());
markRollbackOnly(s);
});
}
use of org.hibernate.stat.Statistics in project hibernate-orm by hibernate.
the class ReadWriteTest method testQueryCacheHitInSameTransaction.
@Test
public void testQueryCacheHitInSameTransaction() throws Exception {
Statistics stats = sessionFactory().getStatistics();
stats.clear();
Item item = new Item("galder", "Galder'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();
s.createQuery("from Item").setCacheable(true).list();
assertEquals(1, stats.getQueryCacheHitCount());
});
withTxSession(s -> s.createQuery("delete from Item").executeUpdate());
}
use of org.hibernate.stat.Statistics in project hibernate-orm by hibernate.
the class ReadWriteTest method testNaturalIdCached.
@Test
public void testNaturalIdCached() throws Exception {
saveSomeCitizens();
// Clear the cache before the transaction begins
cleanupCache();
TIME_SERVICE.advance(1);
withTxSession(s -> {
State france = ReadWriteTest.this.getState(s, "Ile de France");
Criteria criteria = s.createCriteria(Citizen.class);
criteria.add(Restrictions.naturalId().set("ssn", "1234").set("state", france));
criteria.setCacheable(true);
Statistics stats = sessionFactory().getStatistics();
stats.setStatisticsEnabled(true);
stats.clear();
assertEquals("Cache hits should be empty", 0, stats.getNaturalIdCacheHitCount());
List results = criteria.list();
assertEquals(1, results.size());
assertEquals("NaturalId Cache Hits", 0, stats.getNaturalIdCacheHitCount());
assertEquals("NaturalId Cache Misses", 1, stats.getNaturalIdCacheMissCount());
assertEquals("NaturalId Cache Puts", 1, stats.getNaturalIdCachePutCount());
assertEquals("NaturalId Cache Queries", 1, stats.getNaturalIdQueryExecutionCount());
criteria.list();
assertEquals("NaturalId Cache Hits", 0, stats.getNaturalIdCacheHitCount());
assertEquals("NaturalId Cache Misses", 1, stats.getNaturalIdCacheMissCount());
assertEquals("NaturalId Cache Puts", 1, stats.getNaturalIdCachePutCount());
assertEquals("NaturalId Cache Queries", 1, stats.getNaturalIdQueryExecutionCount());
markRollbackOnly(s);
});
}
use of org.hibernate.stat.Statistics in project hibernate-orm by hibernate.
the class ReadWriteTest method testCollectionCache.
@Test
public void testCollectionCache() throws Exception {
final Statistics stats = sessionFactory().getStatistics();
stats.clear();
final Item item = new Item("chris", "Chris's Item");
final Item another = new Item("another", "Owned Item");
item.addItem(another);
withTxSession(s -> {
s.persist(item);
s.persist(another);
});
// The collection has been removed, but we can't add it again immediately using putFromLoad
TIME_SERVICE.advance(1);
withTxSession(s -> {
Item loaded = s.load(Item.class, item.getId());
assertEquals(1, loaded.getItems().size());
});
SecondLevelCacheStatistics cStats = stats.getSecondLevelCacheStatistics(Item.class.getName() + ".items");
assertEquals(1, cStats.getElementCountInMemory());
withTxSession(s -> {
Item loadedWithCachedCollection = (Item) s.load(Item.class, item.getId());
stats.logSummary();
assertEquals(item.getName(), loadedWithCachedCollection.getName());
assertEquals(item.getItems().size(), loadedWithCachedCollection.getItems().size());
assertEquals(1, cStats.getHitCount());
Map cacheEntries = cStats.getEntries();
assertEquals(1, cacheEntries.size());
Item itemElement = loadedWithCachedCollection.getItems().iterator().next();
itemElement.setOwner(null);
loadedWithCachedCollection.getItems().clear();
s.delete(itemElement);
s.delete(loadedWithCachedCollection);
});
}
Aggregations