Search in sources :

Example 6 with Statistics

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

the class ReadWriteTest method testNaturalIdLoaderCached.

@Test
public void testNaturalIdLoaderCached() throws Exception {
    final 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 after insert
    final Citizen citizen = withTxSessionApply(s -> {
        State france = ReadWriteTest.this.getState(s, "Ile de France");
        NaturalIdLoadAccess<Citizen> naturalIdLoader = s.byNaturalId(Citizen.class);
        naturalIdLoader.using("ssn", "1234").using("state", france);
        stats.clear();
        Citizen c = naturalIdLoader.load();
        assertNotNull(c);
        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());
        markRollbackOnly(s);
        return c;
    });
    // TODO: Clear caches manually via cache manager (it's faster!!)
    cleanupCache();
    TIME_SERVICE.advance(1);
    stats.setStatisticsEnabled(true);
    stats.clear();
    //Try NaturalIdLoadAccess
    withTxSession(s -> {
        Citizen loadedCitizen = (Citizen) s.get(Citizen.class, citizen.getId());
        assertNotNull(loadedCitizen);
        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());
        markRollbackOnly(s);
    });
    // Try NaturalIdLoadAccess after load
    withTxSession(s -> {
        State france = ReadWriteTest.this.getState(s, "Ile de France");
        NaturalIdLoadAccess naturalIdLoader = s.byNaturalId(Citizen.class);
        naturalIdLoader.using("ssn", "1234").using("state", france);
        stats.setStatisticsEnabled(true);
        stats.clear();
        Citizen loadedCitizen = (Citizen) naturalIdLoader.load();
        assertNotNull(loadedCitizen);
        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());
        markRollbackOnly(s);
    });
}
Also used : NaturalIdLoadAccess(org.hibernate.NaturalIdLoadAccess) State(org.hibernate.test.cache.infinispan.functional.entities.State) Citizen(org.hibernate.test.cache.infinispan.functional.entities.Citizen) Statistics(org.hibernate.stat.Statistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Test(org.junit.Test)

Example 7 with Statistics

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

the class ReadWriteTest method testEntityCacheContentsAfterEvictAll.

@Test
public void testEntityCacheContentsAfterEvictAll() throws Exception {
    final List<Citizen> citizens = saveSomeCitizens();
    withTxSession(s -> {
        Cache cache = s.getSessionFactory().getCache();
        Statistics stats = sessionFactory().getStatistics();
        SecondLevelCacheStatistics slcStats = stats.getSecondLevelCacheStatistics(Citizen.class.getName());
        assertTrue("2lc entity cache is expected to contain Citizen id = " + citizens.get(0).getId(), cache.containsEntity(Citizen.class, citizens.get(0).getId()));
        assertTrue("2lc entity cache is expected to contain Citizen id = " + citizens.get(1).getId(), cache.containsEntity(Citizen.class, citizens.get(1).getId()));
        assertEquals(2, slcStats.getPutCount());
        cache.evictEntityRegions();
        TIME_SERVICE.advance(1);
        assertEquals(0, slcStats.getElementCountInMemory());
        assertFalse("2lc entity cache is expected to not contain Citizen id = " + citizens.get(0).getId(), cache.containsEntity(Citizen.class, citizens.get(0).getId()));
        assertFalse("2lc entity cache is expected to not contain Citizen id = " + citizens.get(1).getId(), cache.containsEntity(Citizen.class, citizens.get(1).getId()));
        Citizen citizen = s.load(Citizen.class, citizens.get(0).getId());
        assertNotNull(citizen);
        assertNotNull(citizen.getFirstname());
        assertEquals(1, slcStats.getMissCount());
        markRollbackOnly(s);
    });
}
Also used : SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Citizen(org.hibernate.test.cache.infinispan.functional.entities.Citizen) Statistics(org.hibernate.stat.Statistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Cache(org.hibernate.Cache) Test(org.junit.Test)

Example 8 with Statistics

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

the class ReadWriteTest method testQueryCacheHitInSameTransaction.

@Test
public void testQueryCacheHitInSameTransaction() throws Exception {
    Statistics stats = sessionFactory().getStatistics();
    stats.clear();
    Item item = new Item("galder", "Galder'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();
        s.createQuery("from Item").setCacheable(true).list();
        assertEquals(1, stats.getQueryCacheHitCount());
    });
    withTxSession(s -> 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 9 with Statistics

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

the class ReadWriteTest method testNaturalIdCached.

@Test
public void testNaturalIdCached() throws Exception {
    saveSomeCitizens();
    // Clear the cache before the transaction begins
    cleanupCache();
    TIME_SERVICE.advance(1);
    withTxSession(s -> {
        State france = ReadWriteTest.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);
        Statistics stats = sessionFactory().getStatistics();
        stats.setStatisticsEnabled(true);
        stats.clear();
        assertEquals("Cache hits should be empty", 0, stats.getNaturalIdCacheHitCount());
        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());
        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());
        markRollbackOnly(s);
    });
}
Also used : State(org.hibernate.test.cache.infinispan.functional.entities.State) ArrayList(java.util.ArrayList) List(java.util.List) Criteria(org.hibernate.Criteria) Statistics(org.hibernate.stat.Statistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Test(org.junit.Test)

Example 10 with Statistics

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

the class ReadWriteTest method testCollectionCache.

@Test
public void testCollectionCache() throws Exception {
    final Statistics stats = sessionFactory().getStatistics();
    stats.clear();
    final Item item = new Item("chris", "Chris's Item");
    final Item another = new Item("another", "Owned Item");
    item.addItem(another);
    withTxSession(s -> {
        s.persist(item);
        s.persist(another);
    });
    // The collection has been removed, but we can't add it again immediately using putFromLoad
    TIME_SERVICE.advance(1);
    withTxSession(s -> {
        Item loaded = s.load(Item.class, item.getId());
        assertEquals(1, loaded.getItems().size());
    });
    SecondLevelCacheStatistics cStats = stats.getSecondLevelCacheStatistics(Item.class.getName() + ".items");
    assertEquals(1, cStats.getElementCountInMemory());
    withTxSession(s -> {
        Item loadedWithCachedCollection = (Item) s.load(Item.class, item.getId());
        stats.logSummary();
        assertEquals(item.getName(), loadedWithCachedCollection.getName());
        assertEquals(item.getItems().size(), loadedWithCachedCollection.getItems().size());
        assertEquals(1, cStats.getHitCount());
        Map cacheEntries = cStats.getEntries();
        assertEquals(1, cacheEntries.size());
        Item itemElement = loadedWithCachedCollection.getItems().iterator().next();
        itemElement.setOwner(null);
        loadedWithCachedCollection.getItems().clear();
        s.delete(itemElement);
        s.delete(loadedWithCachedCollection);
    });
}
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) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Statistics(org.hibernate.stat.Statistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) 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