Search in sources :

Example 41 with Statistics

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

the class ReadWriteTest method testAddNewOneToManyElementNoInitFlushInitLeaveCacheConsistent.

@Test
public void testAddNewOneToManyElementNoInitFlushInitLeaveCacheConsistent() throws Exception {
    Statistics stats = sessionFactory().getStatistics();
    stats.clear();
    SecondLevelCacheStatistics cStats = stats.getSecondLevelCacheStatistics(Item.class.getName() + ".items");
    ByRef<Long> itemId = new ByRef<>(null);
    saveItem(itemId);
    // create an element for item.bagOfItems
    Item itemElement = new Item();
    itemElement.setName("element");
    itemElement.setDescription("element item");
    withTxSession(s -> {
        Item item = s.get(Item.class, itemId.get());
        assertFalse(Hibernate.isInitialized(item.getBagOfItems()));
        item.addItemToBag(itemElement);
        assertFalse(Hibernate.isInitialized(item.getBagOfItems()));
        s.persist(itemElement);
        s.flush();
        Hibernate.initialize(item.getBagOfItems());
        markRollbackOnly(s);
    });
    withTxSession(s -> {
        Item item = s.get(Item.class, itemId.get());
        Hibernate.initialize(item.getBagOfItems());
        assertTrue(item.getBagOfItems().isEmpty());
        s.delete(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)

Example 42 with Statistics

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

the class ReadWriteTest method testAddNewOneToManyElementInitFlushLeaveCacheConsistent.

@Test
@TestForIssue(jiraKey = "HHH-9231")
public void testAddNewOneToManyElementInitFlushLeaveCacheConsistent() throws Exception {
    Statistics stats = sessionFactory().getStatistics();
    stats.clear();
    SecondLevelCacheStatistics cStats = stats.getSecondLevelCacheStatistics(Item.class.getName() + ".items");
    ByRef<Long> itemId = new ByRef<>(null);
    saveItem(itemId);
    // create an element for item.itsms
    Item itemElement = new Item();
    itemElement.setName("element");
    itemElement.setDescription("element item");
    withTxSession(s -> {
        Item item = s.get(Item.class, itemId.get());
        assertFalse(Hibernate.isInitialized(item.getItems()));
        item.addItem(itemElement);
        assertTrue(Hibernate.isInitialized(item.getItems()));
        s.persist(itemElement);
        s.flush();
        markRollbackOnly(s);
    });
    withTxSession(s -> {
        Item item = s.get(Item.class, itemId.get());
        Hibernate.initialize(item.getItems());
        assertTrue(item.getItems().isEmpty());
        s.delete(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 43 with Statistics

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

the class ReadWriteTest method testAddNewOneToManyElementNoInitFlushLeaveCacheConsistent.

@Test
@TestForIssue(jiraKey = "HHH-9231")
public void testAddNewOneToManyElementNoInitFlushLeaveCacheConsistent() throws Exception {
    Statistics stats = sessionFactory().getStatistics();
    stats.clear();
    SecondLevelCacheStatistics cStats = stats.getSecondLevelCacheStatistics(Item.class.getName() + ".items");
    ByRef<Long> itemId = new ByRef<>(null);
    saveItem(itemId);
    // create an element for item.bagOfItems
    Item itemElement = new Item();
    itemElement.setName("element");
    itemElement.setDescription("element item");
    withTxSession(s -> {
        Item item = s.get(Item.class, itemId.get());
        assertFalse(Hibernate.isInitialized(item.getItems()));
        item.addItemToBag(itemElement);
        assertFalse(Hibernate.isInitialized(item.getBagOfItems()));
        s.persist(itemElement);
        s.flush();
        markRollbackOnly(s);
    });
    withTxSession(s -> {
        Item item = s.get(Item.class, itemId.get());
        Hibernate.initialize(item.getItems());
        assertTrue(item.getItems().isEmpty());
        s.delete(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 44 with Statistics

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

the class ReadOnlyTest method testEmptySecondLevelCacheEntry.

@Test
public void testEmptySecondLevelCacheEntry() throws Exception {
    sessionFactory().getCache().evictCollectionRegion(Item.class.getName() + ".items");
    Statistics stats = sessionFactory().getStatistics();
    stats.clear();
    SecondLevelCacheStatistics statistics = stats.getSecondLevelCacheStatistics(Item.class.getName() + ".items");
    Map cacheEntries = statistics.getEntries();
    assertEquals(0, cacheEntries.size());
}
Also used : SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Statistics(org.hibernate.stat.Statistics) Map(java.util.Map) Test(org.junit.Test)

Example 45 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)

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