use of org.hibernate.stat.Statistics in project hibernate-orm by hibernate.
the class QueryAndSQLTest method testSQLQueryWithManyToOne.
@Test
public void testSQLQueryWithManyToOne() {
cleanupCache();
Night n = new Night();
Calendar c = new GregorianCalendar();
c.set(2000, 2, 2);
Date now = c.getTime();
c.add(Calendar.MONTH, -1);
Date aMonthAgo = c.getTime();
c.add(Calendar.MONTH, 2);
Date inAMonth = c.getTime();
n.setDate(now);
n.setDuration(9999);
Area a = new Area();
a.setName("Paris");
n.setArea(a);
Session s = openSession();
Transaction tx = s.beginTransaction();
s.persist(a);
s.persist(n);
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
Statistics stats = sessionFactory().getStatistics();
stats.setStatisticsEnabled(true);
stats.clear();
Query q = s.getNamedQuery("night&areaCached");
q.setCacheable(true);
List result = q.list();
assertEquals(1, result.size());
assertEquals(1, stats.getQueryCachePutCount());
q.setCacheable(true);
q.list();
assertEquals(1, stats.getQueryCacheHitCount());
Night n2 = (Night) ((Object[]) result.get(0))[0];
assertEquals(n2.getDuration(), n.getDuration());
s.delete(n2.getArea());
s.delete(n2);
tx.commit();
s.close();
}
use of org.hibernate.stat.Statistics in project hibernate-orm by hibernate.
the class EntityUpdateCacheModeIgnoreTest method testCachModeIgnore.
@Test
@TestForIssue(jiraKey = "HHH9739")
public void testCachModeIgnore() {
// Test that there is no interaction with cache except for invalidation when using CacheMode.IGNORE
// From API Doc : CacheMode.IGNORE -> The session will never interact with the cache, except to invalidate cache items when updates occur.
Session s;
Transaction t;
SessionFactory sessionFactory;
Statistics statistics;
// ----------------------------------------------------------------------------------------------
// insert
s = openSession();
s.setCacheMode(CacheMode.IGNORE);
sessionFactory = s.getSessionFactory();
sessionFactory.getCache().evictAllRegions();
statistics = sessionFactory.getStatistics();
statistics.clear();
t = s.beginTransaction();
PurchaseOrder purchaseOrder = new PurchaseOrder(1L, 2L, 1000L);
s.persist(purchaseOrder);
t.commit();
s.close();
assertEquals(0L, statistics.getSecondLevelCacheHitCount());
assertEquals(0L, statistics.getSecondLevelCacheMissCount());
assertEquals(0L, statistics.getSecondLevelCachePutCount());
assertFalse(sessionFactory.getCache().containsEntity(PurchaseOrder.class, 1L));
// ----------------------------------------------------------------------------------------------
// update
s = openSession();
s.setCacheMode(CacheMode.IGNORE);
sessionFactory = s.getSessionFactory();
sessionFactory.getCache().evictAllRegions();
statistics = sessionFactory.getStatistics();
statistics.clear();
t = s.beginTransaction();
PurchaseOrder result = (PurchaseOrder) s.get(PurchaseOrder.class, 1L);
result.setTotalAmount(2000L);
t.commit();
s.close();
assertEquals(0, statistics.getSecondLevelCacheHitCount());
assertEquals(0, statistics.getSecondLevelCacheMissCount());
assertEquals(0, statistics.getSecondLevelCachePutCount());
// the following fails because the cache contains a lock for that entity
//assertFalse(sessionFactory.getCache().containsEntity(PurchaseOrder.class, 1L));
// make sure the updated entity is not found in the cache
s = openSession();
s.setCacheMode(CacheMode.GET);
sessionFactory = s.getSessionFactory();
//sessionFactory.getCache().evictAllRegions();
t = s.beginTransaction();
result = s.get(PurchaseOrder.class, 1L);
assertEquals(2000, result.getTotalAmount().longValue());
t.commit();
s.close();
assertEquals(0, statistics.getSecondLevelCacheHitCount());
assertEquals(1, statistics.getSecondLevelCacheMissCount());
assertEquals(0, statistics.getSecondLevelCachePutCount());
}
use of org.hibernate.stat.Statistics in project hibernate-orm by hibernate.
the class StatisticsTest method testSessionStats.
@Test
public void testSessionStats() throws Exception {
SessionFactory sf = sessionFactory();
Statistics stats = sf.getStatistics();
boolean isStats = stats.isStatisticsEnabled();
stats.clear();
stats.setStatisticsEnabled(true);
Session s = sf.openSession();
assertEquals(1, stats.getSessionOpenCount());
s.close();
assertEquals(1, stats.getSessionCloseCount());
s = sf.openSession();
Transaction tx = s.beginTransaction();
A a = new A();
a.setName("mya");
s.save(a);
a.setName("b");
tx.commit();
s.close();
assertEquals(1, stats.getFlushCount());
s = sf.openSession();
tx = s.beginTransaction();
String hql = "from " + A.class.getName();
Query q = s.createQuery(hql);
q.list();
tx.commit();
s.close();
assertEquals(1, stats.getQueryExecutionCount());
assertEquals(1, stats.getQueryStatistics(hql).getExecutionCount());
stats.setStatisticsEnabled(isStats);
}
use of org.hibernate.stat.Statistics in project hibernate-orm by hibernate.
the class OnDeleteTest method testJoinedSubclass.
@Test
@RequiresDialectFeature(value = DialectChecks.SupportsCircularCascadeDeleteCheck.class, comment = "db/dialect does not support circular cascade delete constraints")
public void testJoinedSubclass() {
Statistics statistics = sessionFactory().getStatistics();
statistics.clear();
Session s = openSession();
Transaction t = s.beginTransaction();
Salesperson mark = new Salesperson();
mark.setName("Mark");
mark.setTitle("internal sales");
mark.setSex('M');
mark.setAddress("buckhead");
mark.setZip("30305");
mark.setCountry("USA");
Person joe = new Person();
joe.setName("Joe");
joe.setAddress("San Francisco");
joe.setZip("XXXXX");
joe.setCountry("USA");
joe.setSex('M');
joe.setSalesperson(mark);
mark.getCustomers().add(joe);
s.save(mark);
t.commit();
assertEquals(statistics.getEntityInsertCount(), 2);
assertEquals(statistics.getPrepareStatementCount(), 5);
statistics.clear();
t = s.beginTransaction();
s.delete(mark);
t.commit();
assertEquals(statistics.getEntityDeleteCount(), 2);
if (getDialect().supportsCascadeDelete()) {
assertEquals(statistics.getPrepareStatementCount(), 1);
}
t = s.beginTransaction();
List names = s.createQuery("select name from Person").list();
assertTrue(names.isEmpty());
t.commit();
s.close();
}
use of org.hibernate.stat.Statistics in project hibernate-orm by hibernate.
the class HibernateCacheTest method testEmptySecondLevelCacheEntry.
@Test
public void testEmptySecondLevelCacheEntry() throws Exception {
sessionFactory().getCache().evictEntityRegion(Item.class.getName());
Statistics stats = sessionFactory().getStatistics();
stats.clear();
SecondLevelCacheStatistics statistics = stats.getSecondLevelCacheStatistics(REGION_PREFIX + Item.class.getName());
Map cacheEntries = statistics.getEntries();
assertThat(cacheEntries.size(), equalTo(0));
}
Aggregations