Search in sources :

Example 21 with Statistics

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);
}
Also used : EntityManager(javax.persistence.EntityManager) Statistics(org.hibernate.stat.Statistics) Test(org.junit.Test)

Example 22 with Statistics

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);
}
Also used : EntityManager(javax.persistence.EntityManager) Statistics(org.hibernate.stat.Statistics) Test(org.junit.Test)

Example 23 with Statistics

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();
}
Also used : NaturalIdLoadAccess(org.hibernate.NaturalIdLoadAccess) Transaction(org.hibernate.Transaction) Statistics(org.hibernate.stat.Statistics) Session(org.hibernate.Session) Test(org.junit.Test)

Example 24 with Statistics

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();
}
Also used : Transaction(org.hibernate.Transaction) List(java.util.List) Criteria(org.hibernate.Criteria) Statistics(org.hibernate.stat.Statistics) Session(org.hibernate.Session) Test(org.junit.Test)

Example 25 with Statistics

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();
}
Also used : Transaction(org.hibernate.Transaction) List(java.util.List) Criteria(org.hibernate.Criteria) Statistics(org.hibernate.stat.Statistics) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

Statistics (org.hibernate.stat.Statistics)61 Test (org.junit.Test)44 SecondLevelCacheStatistics (org.hibernate.stat.SecondLevelCacheStatistics)28 Session (org.hibernate.Session)16 EntityManager (javax.persistence.EntityManager)15 Item (org.hibernate.test.cache.infinispan.functional.entities.Item)14 Transaction (org.hibernate.Transaction)12 VersionedItem (org.hibernate.test.cache.infinispan.functional.entities.VersionedItem)11 OtherItem (org.hibernate.test.cache.infinispan.functional.entities.OtherItem)10 QueryStatistics (org.hibernate.stat.QueryStatistics)9 ByRef (org.infinispan.commons.util.ByRef)8 ArrayList (java.util.ArrayList)7 List (java.util.List)7 Map (java.util.Map)4 CompositeData (javax.management.openmbean.CompositeData)4 TabularData (javax.management.openmbean.TabularData)4 Criteria (org.hibernate.Criteria)4 NaturalIdLoadAccess (org.hibernate.NaturalIdLoadAccess)4 SessionFactory (org.hibernate.SessionFactory)4 TestForIssue (org.hibernate.testing.TestForIssue)4