use of org.hibernate.stat.Statistics in project hibernate-orm by hibernate.
the class FlushAndTransactionTest method testTransactionalOperationsWhenExtended.
@Test
public void testTransactionalOperationsWhenExtended() throws Exception {
Book book = new Book();
book.name = "Le petit prince";
EntityManager em = getOrCreateEntityManager();
Statistics stats = ((HibernateEntityManagerFactory) entityManagerFactory()).getSessionFactory().getStatistics();
stats.clear();
stats.setStatisticsEnabled(true);
em.persist(book);
assertEquals(0, stats.getEntityInsertCount());
em.getTransaction().begin();
em.flush();
em.getTransaction().commit();
assertEquals(1, stats.getEntityInsertCount());
em.clear();
book.name = "Le prince";
book = em.merge(book);
em.refresh(book);
assertEquals(0, stats.getEntityUpdateCount());
em.getTransaction().begin();
em.flush();
em.getTransaction().commit();
assertEquals(0, stats.getEntityUpdateCount());
book.name = "Le prince";
em.getTransaction().begin();
em.find(Book.class, book.id);
em.getTransaction().commit();
assertEquals(1, stats.getEntityUpdateCount());
em.remove(book);
assertEquals(0, stats.getEntityDeleteCount());
em.getTransaction().begin();
em.flush();
em.getTransaction().commit();
assertEquals(1, stats.getEntityDeleteCount());
em.close();
stats.setStatisticsEnabled(false);
}
use of org.hibernate.stat.Statistics in project hibernate-orm by hibernate.
the class FlushAndTransactionTest method testMergeWhenExtended.
@Test
public void testMergeWhenExtended() throws Exception {
Book book = new Book();
book.name = "Le petit prince";
EntityManager em = getOrCreateEntityManager();
Statistics stats = ((HibernateEntityManagerFactory) entityManagerFactory()).getSessionFactory().getStatistics();
em.getTransaction().begin();
em.persist(book);
assertEquals(0, stats.getEntityInsertCount());
em.getTransaction().commit();
//persist and clear
em.clear();
stats.clear();
stats.setStatisticsEnabled(true);
Book bookReloaded = em.find(Book.class, book.id);
book.name = "Le prince";
assertEquals("Merge should use the available entiies in the PC", em.merge(book), bookReloaded);
assertEquals(book.name, bookReloaded.name);
assertEquals(0, stats.getEntityDeleteCount());
assertEquals(0, stats.getEntityInsertCount());
assertEquals("Updates should have been queued", 0, stats.getEntityUpdateCount());
em.getTransaction().begin();
Book bookReReloaded = em.find(Book.class, bookReloaded.id);
assertEquals("reload should return the object in PC", bookReReloaded, bookReloaded);
assertEquals(bookReReloaded.name, bookReloaded.name);
em.getTransaction().commit();
assertEquals(0, stats.getEntityDeleteCount());
assertEquals(0, stats.getEntityInsertCount());
assertEquals("Work on Tx should flush", 1, stats.getEntityUpdateCount());
em.getTransaction().begin();
em.remove(bookReReloaded);
em.getTransaction().commit();
em.close();
stats.setStatisticsEnabled(false);
}
use of org.hibernate.stat.Statistics in project hibernate-orm by hibernate.
the class NaturalIdTest method testNaturalIdLoaderCached.
@Test
public void testNaturalIdLoaderCached() {
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 afterQuery insert
Session s = openSession();
Transaction tx = s.beginTransaction();
State france = this.getState(s, "Ile de France");
NaturalIdLoadAccess naturalIdLoader = s.byNaturalId(Citizen.class);
naturalIdLoader.using("ssn", "1234").using("state", france);
//Not clearing naturalId caches, should be warm from entity loading
stats.clear();
// first query
Citizen citizen = (Citizen) naturalIdLoader.load();
assertNotNull(citizen);
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());
// cleanup
tx.rollback();
s.close();
//Try NaturalIdLoadAccess
s = openSession();
tx = s.beginTransaction();
this.cleanupCache();
stats.setStatisticsEnabled(true);
stats.clear();
// first query
citizen = (Citizen) s.get(Citizen.class, citizen.getId());
assertNotNull(citizen);
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());
// cleanup
tx.rollback();
s.close();
//Try NaturalIdLoadAccess afterQuery load
s = openSession();
tx = s.beginTransaction();
france = this.getState(s, "Ile de France");
naturalIdLoader = s.byNaturalId(Citizen.class);
naturalIdLoader.using("ssn", "1234").using("state", france);
//Not clearing naturalId caches, should be warm from entity loading
stats.setStatisticsEnabled(true);
stats.clear();
// first query
citizen = (Citizen) naturalIdLoader.load();
assertNotNull(citizen);
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());
// cleanup
tx.rollback();
s.close();
}
use of org.hibernate.stat.Statistics in project hibernate-orm by hibernate.
the class NaturalIdTest method testNaturalIdUncached.
@Test
public void testNaturalIdUncached() {
saveSomeCitizens();
Session s = openSession();
Transaction tx = s.beginTransaction();
State france = this.getState(s, "Ile de France");
Criteria criteria = s.createCriteria(Citizen.class);
criteria.add(Restrictions.naturalId().set("ssn", "1234").set("state", france));
criteria.setCacheable(false);
this.cleanupCache();
Statistics stats = sessionFactory().getStatistics();
stats.setStatisticsEnabled(true);
stats.clear();
assertEquals("Cache hits should be empty", 0, stats.getNaturalIdCacheHitCount());
// first query
List results = criteria.list();
assertEquals(1, results.size());
assertEquals("Cache hits should be empty", 0, stats.getNaturalIdCacheHitCount());
assertEquals("Query execution count should be one", 1, stats.getNaturalIdQueryExecutionCount());
// query a second time - result should be cached in session
criteria.list();
assertEquals("Cache hits should be empty", 0, stats.getNaturalIdCacheHitCount());
assertEquals("Second query should not be a miss", 1, stats.getNaturalIdCacheMissCount());
assertEquals("Query execution count should be one", 1, stats.getNaturalIdQueryExecutionCount());
// cleanup
tx.rollback();
s.close();
}
use of org.hibernate.stat.Statistics in project hibernate-orm by hibernate.
the class NaturalIdTest method testNaturalIdCached.
@Test
public void testNaturalIdCached() {
saveSomeCitizens();
Session s = openSession();
Transaction tx = s.beginTransaction();
State france = 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);
this.cleanupCache();
Statistics stats = sessionFactory().getStatistics();
stats.setStatisticsEnabled(true);
stats.clear();
assertEquals("Cache hits should be empty", 0, stats.getNaturalIdCacheHitCount());
assertEquals("Cache puts should be empty", 0, stats.getNaturalIdCachePutCount());
// first query
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());
// query a second time - result should be cached in session
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());
// cleanup
tx.rollback();
s.close();
}
Aggregations