Search in sources :

Example 91 with Statistics

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

Example 92 with Statistics

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

Example 93 with Statistics

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

Example 94 with Statistics

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

Example 95 with Statistics

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

Aggregations

Statistics (org.hibernate.stat.Statistics)97 Test (org.junit.Test)59 Session (org.hibernate.Session)27 SecondLevelCacheStatistics (org.hibernate.stat.SecondLevelCacheStatistics)26 EntityManager (javax.persistence.EntityManager)21 QueryStatistics (org.hibernate.stat.QueryStatistics)19 Item (org.hibernate.test.cache.infinispan.functional.entities.Item)14 Transaction (org.hibernate.Transaction)12 ArrayList (java.util.ArrayList)11 VersionedItem (org.hibernate.test.cache.infinispan.functional.entities.VersionedItem)11 List (java.util.List)10 OtherItem (org.hibernate.test.cache.infinispan.functional.entities.OtherItem)10 SessionFactory (org.hibernate.SessionFactory)9 CacheRegionStatistics (org.hibernate.stat.CacheRegionStatistics)9 ByRef (org.infinispan.commons.util.ByRef)8 TestForIssue (org.hibernate.testing.TestForIssue)6 Date (java.util.Date)5 UserTransaction (javax.transaction.UserTransaction)5 NaturalIdCacheStatistics (org.hibernate.stat.NaturalIdCacheStatistics)5 Test (org.testng.annotations.Test)5