Search in sources :

Example 66 with Statistics

use of org.hibernate.stat.Statistics in project hibernate-orm by hibernate.

the class ReadWriteTest method testQueryCache.

@Test
public void testQueryCache() throws Exception {
    Statistics stats = sessionFactory().getStatistics();
    stats.clear();
    Item item = new Item("chris", "Chris's Item");
    withTxSession(s -> s.persist(item));
    // Delay added to guarantee that query cache results won't be considered
    // as not up to date due to persist session and query results from first
    // query happening simultaneously.
    TIME_SERVICE.advance(1);
    withTxSession(s -> s.createQuery("from Item").setCacheable(true).list());
    withTxSession(s -> {
        s.createQuery("from Item").setCacheable(true).list();
        assertEquals(1, stats.getQueryCacheHitCount());
        s.createQuery("delete from Item").executeUpdate();
    });
}
Also used : VersionedItem(org.hibernate.test.cache.infinispan.functional.entities.VersionedItem) Item(org.hibernate.test.cache.infinispan.functional.entities.Item) OtherItem(org.hibernate.test.cache.infinispan.functional.entities.OtherItem) Statistics(org.hibernate.stat.Statistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Test(org.junit.Test)

Example 67 with Statistics

use of org.hibernate.stat.Statistics in project hibernate-orm by hibernate.

the class ReadWriteTest method testStaleWritesLeaveCacheConsistent.

@Test
public void testStaleWritesLeaveCacheConsistent() throws Exception {
    Statistics stats = sessionFactory().getStatistics();
    stats.clear();
    ByRef<VersionedItem> itemRef = new ByRef<>(null);
    withTxSession(s -> {
        VersionedItem item = new VersionedItem();
        item.setName("steve");
        item.setDescription("steve's item");
        s.save(item);
        itemRef.set(item);
    });
    final VersionedItem item = itemRef.get();
    Long initialVersion = item.getVersion();
    // manually revert the version property
    item.setVersion(new Long(item.getVersion().longValue() - 1));
    try {
        withTxSession(s -> s.update(item));
        fail("expected stale write to fail");
    } catch (Exception e) {
        log.debug("Rollback was expected", e);
    }
    // check the version value in the cache...
    SecondLevelCacheStatistics slcs = stats.getSecondLevelCacheStatistics(VersionedItem.class.getName());
    Object entry = slcs.getEntries().get(item.getId());
    Long cachedVersionValue;
    cachedVersionValue = (Long) ((CacheEntry) entry).getVersion();
    assertEquals(initialVersion.longValue(), cachedVersionValue.longValue());
    withTxSession(s -> {
        VersionedItem item2 = s.load(VersionedItem.class, item.getId());
        s.delete(item2);
    });
}
Also used : VersionedItem(org.hibernate.test.cache.infinispan.functional.entities.VersionedItem) ByRef(org.infinispan.commons.util.ByRef) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) CacheEntry(org.hibernate.cache.spi.entry.CacheEntry) Statistics(org.hibernate.stat.Statistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Test(org.junit.Test)

Example 68 with Statistics

use of org.hibernate.stat.Statistics in project hibernate-orm by hibernate.

the class ReadWriteTest method testPersistEntityFlushEvictGetRollbackNotInEntityCache.

@Test
@TestForIssue(jiraKey = "HHH-5690")
public void testPersistEntityFlushEvictGetRollbackNotInEntityCache() throws Exception {
    Statistics stats = sessionFactory().getStatistics();
    stats.clear();
    SecondLevelCacheStatistics slcs = stats.getSecondLevelCacheStatistics(Item.class.getName());
    ByRef<Long> itemId = new ByRef<>(null);
    withTxSession(s -> {
        Item item = new Item();
        item.setName("steve");
        item.setDescription("steve's item");
        s.persist(item);
        s.flush();
        itemId.set(item.getId());
        s.evict(item);
        assertEquals(slcs.getHitCount(), 0);
        item = s.get(Item.class, item.getId());
        assertNotNull(item);
        markRollbackOnly(s);
    });
    // item should not be in entity cache.
    //slcs = stats.getSecondLevelCacheStatistics( Item.class.getName() );
    assertEquals(Collections.EMPTY_MAP, slcs.getEntries());
    withTxSession(s -> {
        Item item = s.get(Item.class, itemId.get());
        assertNull(item);
    });
}
Also used : VersionedItem(org.hibernate.test.cache.infinispan.functional.entities.VersionedItem) Item(org.hibernate.test.cache.infinispan.functional.entities.Item) OtherItem(org.hibernate.test.cache.infinispan.functional.entities.OtherItem) ByRef(org.infinispan.commons.util.ByRef) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Statistics(org.hibernate.stat.Statistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 69 with Statistics

use of org.hibernate.stat.Statistics in project hibernate-orm by hibernate.

the class ReadWriteTest method testPersistEntityFlushRollbackNotInEntityCache.

@Test
@TestForIssue(jiraKey = "HHH-5690")
public void testPersistEntityFlushRollbackNotInEntityCache() throws Exception {
    Statistics stats = sessionFactory().getStatistics();
    stats.clear();
    SecondLevelCacheStatistics slcs = stats.getSecondLevelCacheStatistics(Item.class.getName());
    ByRef<Long> itemId = new ByRef<>(null);
    withTxSession(s -> {
        Item item = new Item();
        item.setName("steve");
        item.setDescription("steve's item");
        s.persist(item);
        s.flush();
        itemId.set(item.getId());
        markRollbackOnly(s);
    });
    // item should not be in entity cache.
    assertEquals(Collections.EMPTY_MAP, slcs.getEntries());
    withTxSession(s -> {
        Item item = s.get(Item.class, itemId.get());
        assertNull(item);
    });
}
Also used : VersionedItem(org.hibernate.test.cache.infinispan.functional.entities.VersionedItem) Item(org.hibernate.test.cache.infinispan.functional.entities.Item) OtherItem(org.hibernate.test.cache.infinispan.functional.entities.OtherItem) ByRef(org.infinispan.commons.util.ByRef) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Statistics(org.hibernate.stat.Statistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 70 with Statistics

use of org.hibernate.stat.Statistics in project hibernate-orm by hibernate.

the class NaturalIdOnSingleManyToOneTest method testManyToOneNaturalIdCached.

@Test
public void testManyToOneNaturalIdCached() {
    NaturalIdOnManyToOne singleManyToOne = new NaturalIdOnManyToOne();
    Citizen c1 = new Citizen();
    c1.setFirstname("Emmanuel");
    c1.setLastname("Bernard");
    c1.setSsn("1234");
    State france = new State();
    france.setName("Ile de France");
    c1.setState(france);
    singleManyToOne.setCitizen(c1);
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    s.persist(france);
    s.persist(c1);
    s.persist(singleManyToOne);
    tx.commit();
    s.close();
    s.getSessionFactory().getCache().evictNaturalIdRegions();
    Statistics stats = sessionFactory().getStatistics();
    stats.setStatisticsEnabled(true);
    stats.clear();
    assertEquals("NaturalId cache puts should be zero", 0, stats.getNaturalIdCachePutCount());
    assertEquals("NaturalId cache hits should be zero", 0, stats.getNaturalIdCacheHitCount());
    assertEquals("NaturalId Cache Puts", 0, stats.getNaturalIdCachePutCount());
    assertEquals("NaturalId cache misses should be zero", 0, stats.getNaturalIdCacheMissCount());
    s = openSession();
    tx = s.beginTransaction();
    Criteria criteria = s.createCriteria(NaturalIdOnManyToOne.class);
    criteria.add(Restrictions.naturalId().set("citizen", c1));
    criteria.setCacheable(true);
    // first query
    List results = criteria.list();
    assertEquals(1, results.size());
    assertEquals("NaturalId Cache Hits", 0, stats.getNaturalIdCacheHitCount());
    assertEquals("NaturalId Cache Misses", 1, stats.getNaturalIdCacheMissCount());
    // one for Citizen, one for NaturalIdOnManyToOne
    assertEquals("NaturalId Cache Puts", 2, stats.getNaturalIdCachePutCount());
    assertEquals("NaturalId Cache Queries", 1, stats.getNaturalIdQueryExecutionCount());
    // query a second time - result should be in session cache
    criteria.list();
    assertEquals("NaturalId Cache Hits", 0, stats.getNaturalIdCacheHitCount());
    assertEquals("NaturalId Cache Misses", 1, stats.getNaturalIdCacheMissCount());
    assertEquals("NaturalId Cache Puts", 2, 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)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