Search in sources :

Example 26 with Statistics

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

Example 27 with Statistics

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

Example 28 with Statistics

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

Example 29 with Statistics

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

Example 30 with Statistics

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));
}
Also used : Item(org.hibernate.test.domain.Item) VersionedItem(org.hibernate.test.domain.VersionedItem) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) QueryStatistics(org.hibernate.stat.QueryStatistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Statistics(org.hibernate.stat.Statistics) Map(java.util.Map) 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