use of org.hibernate.stat.Statistics in project hibernate-orm by hibernate.
the class ImmutableEntityNaturalIdTest method testImmutableNaturalIdLifecycle.
@Test
public void testImmutableNaturalIdLifecycle() {
Statistics stats = sessionFactory().getStatistics();
stats.setStatisticsEnabled(true);
stats.clear();
assertEquals("Cache hits should be empty", 0, stats.getNaturalIdCacheHitCount());
assertEquals("Cache misses should be empty", 0, stats.getNaturalIdCacheMissCount());
assertEquals("Cache put should be empty", 0, stats.getNaturalIdCachePutCount());
assertEquals("Query count should be empty", 0, stats.getNaturalIdQueryExecutionCount());
Building b1 = new Building();
b1.setName("Computer Science");
b1.setAddress("1210 W. Dayton St.");
b1.setCity("Madison");
b1.setState("WI");
Session s = openSession();
Transaction tx = s.beginTransaction();
s.persist(b1);
tx.commit();
s.close();
assertEquals("Cache hits should be empty", 0, stats.getNaturalIdCacheHitCount());
assertEquals("Cache misses should be empty", 0, stats.getNaturalIdCacheMissCount());
assertEquals("Cache put should be one after insert", 1, stats.getNaturalIdCachePutCount());
assertEquals("Query count should be empty", 0, stats.getNaturalIdQueryExecutionCount());
s = openSession();
tx = s.beginTransaction();
// Clear caches and reset cache stats
s.getSessionFactory().getCache().evictNaturalIdRegions();
stats.clear();
NaturalIdLoadAccess naturalIdLoader = s.byNaturalId(Building.class);
naturalIdLoader.using("address", "1210 W. Dayton St.").using("city", "Madison").using("state", "WI");
// first query
Building building = (Building) naturalIdLoader.load();
assertNotNull(building);
assertEquals("Cache hits should be empty", 0, stats.getNaturalIdCacheHitCount());
assertEquals("Cache misses should be one after first query", 1, stats.getNaturalIdCacheMissCount());
assertEquals("Cache put should be one after first query", 1, stats.getNaturalIdCachePutCount());
assertEquals("Query count should be one after first query", 1, stats.getNaturalIdQueryExecutionCount());
// cleanup
tx.rollback();
s.close();
// Try two, should be a cache hit
s = openSession();
tx = s.beginTransaction();
naturalIdLoader = s.byNaturalId(Building.class);
naturalIdLoader.using("address", "1210 W. Dayton St.").using("city", "Madison").using("state", "WI");
// second query
building = (Building) naturalIdLoader.load();
assertNotNull(building);
assertEquals("Cache hits should be one after second query", 1, stats.getNaturalIdCacheHitCount());
assertEquals("Cache misses should be one after second query", 1, stats.getNaturalIdCacheMissCount());
assertEquals("Cache put should be one after second query", 1, stats.getNaturalIdCachePutCount());
assertEquals("Query count should be one after second query", 1, stats.getNaturalIdQueryExecutionCount());
// Try Deleting
s.delete(building);
// third query
building = (Building) naturalIdLoader.load();
assertNull(building);
assertEquals("Cache hits should be one after second query", 1, stats.getNaturalIdCacheHitCount());
assertEquals("Cache misses should be two after second query", 2, stats.getNaturalIdCacheMissCount());
assertEquals("Cache put should be one after second query", 2, stats.getNaturalIdCachePutCount());
assertEquals("Query count should be two after second query", 2, stats.getNaturalIdQueryExecutionCount());
// cleanup
tx.commit();
s.close();
// Try three, should be db lookup and miss
s = openSession();
tx = s.beginTransaction();
naturalIdLoader = s.byNaturalId(Building.class);
naturalIdLoader.using("address", "1210 W. Dayton St.").using("city", "Madison").using("state", "WI");
// second query
building = (Building) naturalIdLoader.load();
assertNull(building);
assertEquals("Cache hits should be one after third query", 1, stats.getNaturalIdCacheHitCount());
assertEquals("Cache misses should be one after third query", 3, stats.getNaturalIdCacheMissCount());
assertEquals("Cache put should be one after third query", 2, stats.getNaturalIdCachePutCount());
assertEquals("Query count should be one after third query", 3, stats.getNaturalIdQueryExecutionCount());
// cleanup
tx.rollback();
s.close();
}
use of org.hibernate.stat.Statistics in project hibernate-orm by hibernate.
the class NaturalIdTest method testNaturalIdLoaderNotCached.
@Test
public void testNaturalIdLoaderNotCached() {
saveSomeCitizens();
Session s = openSession();
Transaction tx = s.beginTransaction();
State france = this.getState(s, "Ile de France");
final NaturalIdLoadAccess naturalIdLoader = s.byNaturalId(Citizen.class);
naturalIdLoader.using("ssn", "1234").using("state", france);
// NaturalId cache gets populated during entity loading, need to clear it out
this.cleanupCache();
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());
// first query
Citizen citizen = (Citizen) naturalIdLoader.load();
assertNotNull(citizen);
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());
// cleanup
tx.rollback();
s.close();
}
use of org.hibernate.stat.Statistics in project redisson by redisson.
the class TransactionalTest method testQuery.
@Test
public void testQuery() {
Statistics stats = sessionFactory().getStatistics();
Session s = openSession();
s.beginTransaction();
ItemTransactional item = new ItemTransactional("data");
item.getEntries().addAll(Arrays.asList("a", "b", "c"));
s.save(item);
s.flush();
s.getTransaction().commit();
s = openSession();
s.beginTransaction();
Query query = s.getNamedQuery("testQuery");
query.setCacheable(true);
query.setCacheRegion("myTestQuery");
query.setParameter("name", "data");
item = (ItemTransactional) query.uniqueResult();
s.getTransaction().commit();
s.close();
Assert.assertEquals(1, stats.getDomainDataRegionStatistics("myTestQuery").getPutCount());
s = openSession();
s.beginTransaction();
Query query2 = s.getNamedQuery("testQuery");
query2.setCacheable(true);
query2.setCacheRegion("myTestQuery");
query2.setParameter("name", "data");
item = (ItemTransactional) query2.uniqueResult();
s.delete(item);
s.getTransaction().commit();
s.close();
Assert.assertEquals(1, stats.getDomainDataRegionStatistics("myTestQuery").getHitCount());
stats.logSummary();
}
use of org.hibernate.stat.Statistics in project redisson by redisson.
the class ReadWriteTest method testUpdateWithRefreshThenRollback.
@Test
public void testUpdateWithRefreshThenRollback() {
Statistics stats = sessionFactory().getStatistics();
Long id = null;
Session s = openSession();
s.beginTransaction();
ItemReadWrite item = new ItemReadWrite("data");
id = (Long) s.save(item);
s.flush();
s.getTransaction().commit();
Assert.assertEquals(1, stats.getDomainDataRegionStatistics("item").getPutCount());
s = openSession();
s.beginTransaction();
item = s.get(ItemReadWrite.class, id);
item.setName("newdata");
s.update(item);
s.flush();
s.refresh(item);
s.getTransaction().rollback();
s.clear();
s.close();
s = openSession();
s.beginTransaction();
item = s.get(ItemReadWrite.class, id);
Assert.assertEquals("data", item.getName());
s.delete(item);
s.getTransaction().commit();
s.close();
Assert.assertEquals(1, stats.getDomainDataRegionStatistics("item").getHitCount());
}
use of org.hibernate.stat.Statistics in project redisson by redisson.
the class ReadWriteTest method testQuery.
@Test
public void testQuery() {
Statistics stats = sessionFactory().getStatistics();
Session s = openSession();
s.beginTransaction();
ItemReadWrite item = new ItemReadWrite("data");
item.getEntries().addAll(Arrays.asList("a", "b", "c"));
s.save(item);
s.flush();
s.getTransaction().commit();
s = openSession();
s.beginTransaction();
Query<ItemReadWrite> query = s.getNamedQuery("testQuery");
query.setCacheable(true);
query.setCacheRegion("myTestQuery");
query.setParameter("name", "data");
item = query.uniqueResult();
s.getTransaction().commit();
s.close();
Assert.assertEquals(1, stats.getDomainDataRegionStatistics("myTestQuery").getPutCount());
s = openSession();
s.beginTransaction();
Query<ItemReadWrite> query2 = s.getNamedQuery("testQuery");
query2.setCacheable(true);
query2.setCacheRegion("myTestQuery");
query2.setParameter("name", "data");
item = query2.uniqueResult();
s.delete(item);
s.getTransaction().commit();
s.close();
Assert.assertEquals(1, stats.getDomainDataRegionStatistics("myTestQuery").getHitCount());
stats.logSummary();
}
Aggregations