Search in sources :

Example 11 with Statistics

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

the class ReadWriteTest method testAddNewManyToManyPropertyRefNoInitFlushInitLeaveCacheConsistent.

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

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

the class ReadWriteTest method testQueryCacheInvalidation.

@Test
public void testQueryCacheInvalidation() throws Exception {
    Statistics stats = sessionFactory().getStatistics();
    stats.clear();
    SecondLevelCacheStatistics slcs = stats.getSecondLevelCacheStatistics(Item.class.getName());
    sessionFactory().getCache().evictEntityRegion(Item.class.getName());
    TIME_SERVICE.advance(1);
    assertEquals(0, slcs.getPutCount());
    assertEquals(0, slcs.getElementCountInMemory());
    assertEquals(0, slcs.getEntries().size());
    ByRef<Long> idRef = new ByRef<>(null);
    withTxSession(s -> {
        Item item = new Item();
        item.setName("widget");
        item.setDescription("A really top-quality, full-featured widget.");
        s.persist(item);
        idRef.set(item.getId());
    });
    assertEquals(1, slcs.getPutCount());
    assertEquals(1, slcs.getElementCountInMemory());
    assertEquals(1, slcs.getEntries().size());
    withTxSession(s -> {
        Item item = s.get(Item.class, idRef.get());
        assertEquals(slcs.getHitCount(), 1);
        assertEquals(slcs.getMissCount(), 0);
        item.setDescription("A bog standard item");
    });
    assertEquals(slcs.getPutCount(), 2);
    CacheEntry entry = (CacheEntry) slcs.getEntries().get(idRef.get());
    Serializable[] ser = entry.getDisassembledState();
    assertTrue(ser[0].equals("widget"));
    assertTrue(ser[1].equals("A bog standard item"));
    withTxSession(s -> {
        Item item = s.load(Item.class, idRef.get());
        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) Serializable(java.io.Serializable) 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 13 with Statistics

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

the class JndiRegionFactoryTest method addEntityCheckCache.

private void addEntityCheckCache(SessionFactoryImplementor sessionFactory) throws Exception {
    Item item = new Item("chris", "Chris's Item");
    withTxSession(s -> s.persist(item));
    withTxSession(s -> {
        Item found = s.load(Item.class, item.getId());
        Statistics stats = sessionFactory.getStatistics();
        log.info(stats.toString());
        assertEquals(item.getDescription(), found.getDescription());
        assertEquals(0, stats.getSecondLevelCacheMissCount());
        assertEquals(1, stats.getSecondLevelCacheHitCount());
        s.delete(found);
    });
}
Also used : Item(org.hibernate.test.cache.infinispan.functional.entities.Item) Statistics(org.hibernate.stat.Statistics)

Example 14 with Statistics

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

the class JBossStandaloneJtaExampleTest method testPersistAndLoadUnderJta.

@Test
public void testPersistAndLoadUnderJta() throws Exception {
    Item item;
    SessionFactory sessionFactory = buildSessionFactory();
    try {
        UserTransaction ut = (UserTransaction) ctx.lookup("UserTransaction");
        ut.begin();
        try {
            Session session = sessionFactory.openSession();
            assertTrue(session.getTransaction().isActive());
            item = new Item("anItem", "An item owned by someone");
            session.persist(item);
            // IMO the flush should not be necessary, but session.close() does not flush
            // and the item is not persisted.
            session.flush();
            session.close();
        } catch (Exception e) {
            ut.setRollbackOnly();
            throw e;
        } finally {
            if (ut.getStatus() == Status.STATUS_ACTIVE)
                ut.commit();
            else
                ut.rollback();
        }
        ut = (UserTransaction) ctx.lookup("UserTransaction");
        ut.begin();
        try {
            Session session = sessionFactory.openSession();
            assertTrue(session.getTransaction().isActive());
            Item found = (Item) session.load(Item.class, item.getId());
            Statistics stats = session.getSessionFactory().getStatistics();
            log.info(stats.toString());
            assertEquals(item.getDescription(), found.getDescription());
            assertEquals(0, stats.getSecondLevelCacheMissCount());
            assertEquals(1, stats.getSecondLevelCacheHitCount());
            session.delete(found);
            // IMO the flush should not be necessary, but session.close() does not flush
            // and the item is not deleted.
            session.flush();
            session.close();
        } catch (Exception e) {
            ut.setRollbackOnly();
            throw e;
        } finally {
            if (ut.getStatus() == Status.STATUS_ACTIVE)
                ut.commit();
            else
                ut.rollback();
        }
        ut = (UserTransaction) ctx.lookup("UserTransaction");
        ut.begin();
        try {
            Session session = sessionFactory.openSession();
            assertTrue(session.getTransaction().isActive());
            assertNull(session.get(Item.class, item.getId()));
            session.close();
        } catch (Exception e) {
            ut.setRollbackOnly();
            throw e;
        } finally {
            if (ut.getStatus() == Status.STATUS_ACTIVE)
                ut.commit();
            else
                ut.rollback();
        }
    } finally {
        if (sessionFactory != null)
            sessionFactory.close();
    }
}
Also used : SessionFactory(org.hibernate.SessionFactory) UserTransaction(javax.transaction.UserTransaction) Item(org.hibernate.test.cache.infinispan.functional.entities.Item) Statistics(org.hibernate.stat.Statistics) NameNotFoundException(javax.naming.NameNotFoundException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 15 with Statistics

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

the class SessionStatsTest method testSessionStatistics.

@Test
public void testSessionStatistics() throws Exception {
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    Statistics stats = sessionFactory().getStatistics();
    stats.clear();
    boolean isStats = stats.isStatisticsEnabled();
    stats.setStatisticsEnabled(true);
    Continent europe = fillDb(s);
    tx.commit();
    s.clear();
    tx = s.beginTransaction();
    SessionStatistics sessionStats = s.getStatistics();
    assertEquals(0, sessionStats.getEntityKeys().size());
    assertEquals(0, sessionStats.getEntityCount());
    assertEquals(0, sessionStats.getCollectionKeys().size());
    assertEquals(0, sessionStats.getCollectionCount());
    europe = (Continent) s.get(Continent.class, europe.getId());
    Hibernate.initialize(europe.getCountries());
    Hibernate.initialize(europe.getCountries().iterator().next());
    assertEquals(2, sessionStats.getEntityKeys().size());
    assertEquals(2, sessionStats.getEntityCount());
    assertEquals(1, sessionStats.getCollectionKeys().size());
    assertEquals(1, sessionStats.getCollectionCount());
    tx.commit();
    s.close();
    stats.setStatisticsEnabled(isStats);
}
Also used : SessionStatistics(org.hibernate.stat.SessionStatistics) Transaction(org.hibernate.Transaction) SessionStatistics(org.hibernate.stat.SessionStatistics) Statistics(org.hibernate.stat.Statistics) Session(org.hibernate.Session) 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