Search in sources :

Example 26 with SecondLevelCacheStatistics

use of org.hibernate.stat.SecondLevelCacheStatistics in project wildfly by wildfly.

the class StatefulBean method getEmployeesInMemory.

@Override
public long getEmployeesInMemory() {
    Session session = em.unwrap(Session.class);
    String[] entityRegionNames = session.getSessionFactory().getStatistics().getSecondLevelCacheRegionNames();
    for (String name : entityRegionNames) {
        if (name.contains(Employee.class.getName())) {
            SecondLevelCacheStatistics stats = session.getSessionFactory().getStatistics().getSecondLevelCacheStatistics(name);
            return stats.getElementCountInMemory();
        }
    }
    return -1;
}
Also used : SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Session(org.hibernate.Session)

Example 27 with SecondLevelCacheStatistics

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

the class HibernateCacheTest method testStaleWritesLeaveCacheConsistent.

@Test
public void testStaleWritesLeaveCacheConsistent() {
    Session s = sessionFactory().openSession();
    Transaction txn = s.beginTransaction();
    VersionedItem item = new VersionedItem();
    item.setName("steve");
    item.setDescription("steve's item");
    s.save(item);
    txn.commit();
    s.close();
    Long initialVersion = item.getVersion();
    // manually revert the version property
    item.setVersion(item.getVersion() - 1);
    try {
        s = sessionFactory().openSession();
        txn = s.beginTransaction();
        s.update(item);
        txn.commit();
        s.close();
        fail("expected stale write to fail");
    } catch (Throwable expected) {
        // expected behavior here
        if (txn != null) {
            try {
                txn.rollback();
            } catch (Throwable ignore) {
            }
        }
    } finally {
        if (s != null && s.isOpen()) {
            try {
                s.close();
            } catch (Throwable ignore) {
            }
        }
    }
    // check the version value in the cache...
    SecondLevelCacheStatistics slcs = sessionFactory().getStatistics().getSecondLevelCacheStatistics(REGION_PREFIX + VersionedItem.class.getName());
    assertNotNull(slcs);
    final Map entries = slcs.getEntries();
    Object entry = entries.get(item.getId());
    Long cachedVersionValue;
    if (entry instanceof SoftLock) {
    //FIXME don't know what to test here
    //cachedVersionValue = new Long( ( (ReadWriteCache.Lock) entry).getUnlockTimestamp() );
    } else {
        cachedVersionValue = (Long) ((Map) entry).get("_version");
        assertThat(initialVersion, equalTo(cachedVersionValue));
    }
    // cleanup
    s = sessionFactory().openSession();
    txn = s.beginTransaction();
    item = (VersionedItem) s.load(VersionedItem.class, item.getId());
    s.delete(item);
    txn.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) VersionedItem(org.hibernate.test.domain.VersionedItem) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Map(java.util.Map) Session(org.hibernate.Session) SoftLock(org.hibernate.cache.spi.access.SoftLock) Test(org.junit.Test)

Example 28 with SecondLevelCacheStatistics

use of org.hibernate.stat.SecondLevelCacheStatistics in project ignite by apache.

the class HibernateL2CacheExample method printStats.

/**
     * Prints Hibernate L2 cache statistics to standard output.
     *
     * @param sesFactory Hibernate {@link SessionFactory}, for which to print
     *                   statistics.
     */
private static void printStats(SessionFactory sesFactory) {
    System.out.println("=== Hibernate L2 cache statistics ===");
    for (String entityName : ENTITY_NAMES) {
        System.out.println("\tEntity: " + entityName);
        SecondLevelCacheStatistics stats = sesFactory.getStatistics().getSecondLevelCacheStatistics(entityName);
        System.out.println("\t\tPuts: " + stats.getPutCount());
        System.out.println("\t\tHits: " + stats.getHitCount());
        System.out.println("\t\tMisses: " + stats.getMissCount());
    }
    System.out.println("=====================================");
}
Also used : SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics)

Example 29 with SecondLevelCacheStatistics

use of org.hibernate.stat.SecondLevelCacheStatistics in project ignite by apache.

the class HibernateL2CacheSelfTest method assertCollectionCache.

/**
     * @param sesFactory Session factory.
     * @param idToChildCnt Number of children per entity.
     * @param expHit Expected cache hits.
     * @param expMiss Expected cache misses.
     */
@SuppressWarnings("unchecked")
private void assertCollectionCache(SessionFactory sesFactory, Map<Integer, Integer> idToChildCnt, int expHit, int expMiss) {
    sesFactory.getStatistics().clear();
    Session ses = sesFactory.openSession();
    try {
        for (Map.Entry<Integer, Integer> e : idToChildCnt.entrySet()) {
            Entity entity = (Entity) ses.load(Entity.class, e.getKey());
            assertEquals((int) e.getValue(), entity.getChildren().size());
        }
    } finally {
        ses.close();
    }
    SecondLevelCacheStatistics stats = sesFactory.getStatistics().getSecondLevelCacheStatistics(CHILD_COLLECTION_REGION);
    assertEquals(expHit, stats.getHitCount());
    assertEquals(expMiss, stats.getMissCount());
}
Also used : SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Map(java.util.Map) HashMap(java.util.HashMap) Session(org.hibernate.Session)

Example 30 with SecondLevelCacheStatistics

use of org.hibernate.stat.SecondLevelCacheStatistics in project ignite by apache.

the class HibernateL2CacheSelfTest method testRegionClear.

/**
     * @param accessType Cache access type.
     * @throws Exception If failed.
     */
private void testRegionClear(AccessType accessType) throws Exception {
    createSessionFactories(accessType);
    try {
        final int ENTITY_CNT = 100;
        Session ses = sesFactory1.openSession();
        try {
            Transaction tx = ses.beginTransaction();
            for (int i = 0; i < ENTITY_CNT; i++) {
                Entity e = new Entity(i, "name-" + i);
                Collection<ChildEntity> children = new ArrayList<>();
                for (int j = 0; j < 3; j++) children.add(new ChildEntity());
                e.setChildren(children);
                ses.save(e);
            }
            tx.commit();
        } finally {
            ses.close();
        }
        loadEntities(sesFactory2, ENTITY_CNT);
        SecondLevelCacheStatistics stats1 = sesFactory1.getStatistics().getSecondLevelCacheStatistics(ENTITY_NAME);
        SecondLevelCacheStatistics stats2 = sesFactory2.getStatistics().getSecondLevelCacheStatistics(ENTITY_NAME);
        NaturalIdCacheStatistics idStats1 = sesFactory1.getStatistics().getNaturalIdCacheStatistics(NATURAL_ID_REGION);
        NaturalIdCacheStatistics idStats2 = sesFactory2.getStatistics().getNaturalIdCacheStatistics(NATURAL_ID_REGION);
        SecondLevelCacheStatistics colStats1 = sesFactory1.getStatistics().getSecondLevelCacheStatistics(CHILD_COLLECTION_REGION);
        SecondLevelCacheStatistics colStats2 = sesFactory2.getStatistics().getSecondLevelCacheStatistics(CHILD_COLLECTION_REGION);
        assertEquals(ENTITY_CNT, stats1.getElementCountInMemory());
        assertEquals(ENTITY_CNT, stats2.getElementCountInMemory());
        assertEquals(ENTITY_CNT, idStats1.getElementCountInMemory());
        assertEquals(ENTITY_CNT, idStats2.getElementCountInMemory());
        assertEquals(ENTITY_CNT, colStats1.getElementCountInMemory());
        assertEquals(ENTITY_CNT, colStats2.getElementCountInMemory());
        // Test cache is cleared after update query.
        ses = sesFactory1.openSession();
        try {
            Transaction tx = ses.beginTransaction();
            ses.createQuery("delete from " + ENTITY_NAME + " where name='no such name'").executeUpdate();
            ses.createQuery("delete from " + ChildEntity.class.getName() + " where id=-1").executeUpdate();
            tx.commit();
        } finally {
            ses.close();
        }
        assertEquals(0, stats1.getElementCountInMemory());
        assertEquals(0, stats2.getElementCountInMemory());
        assertEquals(0, idStats1.getElementCountInMemory());
        assertEquals(0, idStats2.getElementCountInMemory());
        assertEquals(0, colStats1.getElementCountInMemory());
        assertEquals(0, colStats2.getElementCountInMemory());
        // Load some data in cache.
        loadEntities(sesFactory1, 10);
        assertEquals(10, stats1.getElementCountInMemory());
        assertEquals(10, stats2.getElementCountInMemory());
        assertEquals(10, idStats1.getElementCountInMemory());
        assertEquals(10, idStats2.getElementCountInMemory());
        // Test evictAll method.
        sesFactory2.getCache().evictEntityRegion(ENTITY_NAME);
        assertEquals(0, stats1.getElementCountInMemory());
        assertEquals(0, stats2.getElementCountInMemory());
        sesFactory2.getCache().evictNaturalIdRegion(ENTITY_NAME);
        assertEquals(0, idStats1.getElementCountInMemory());
        assertEquals(0, idStats2.getElementCountInMemory());
        sesFactory2.getCache().evictCollectionRegion(CHILD_COLLECTION_REGION);
        assertEquals(0, colStats1.getElementCountInMemory());
        assertEquals(0, colStats2.getElementCountInMemory());
    } finally {
        cleanup();
    }
}
Also used : NaturalIdCacheStatistics(org.hibernate.stat.NaturalIdCacheStatistics) Transaction(org.hibernate.Transaction) ArrayList(java.util.ArrayList) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Session(org.hibernate.Session)

Aggregations

SecondLevelCacheStatistics (org.hibernate.stat.SecondLevelCacheStatistics)32 Test (org.junit.Test)21 Statistics (org.hibernate.stat.Statistics)18 Session (org.hibernate.Session)11 Map (java.util.Map)9 VersionedItem (org.hibernate.test.cache.infinispan.functional.entities.VersionedItem)9 Item (org.hibernate.test.cache.infinispan.functional.entities.Item)8 OtherItem (org.hibernate.test.cache.infinispan.functional.entities.OtherItem)8 ByRef (org.infinispan.commons.util.ByRef)8 Transaction (org.hibernate.Transaction)6 QueryStatistics (org.hibernate.stat.QueryStatistics)5 EntityManager (javax.persistence.EntityManager)4 TestForIssue (org.hibernate.testing.TestForIssue)4 HashMap (java.util.HashMap)3 VersionedItem (org.hibernate.test.domain.VersionedItem)3 StaleStateException (org.hibernate.StaleStateException)2 CacheEntry (org.hibernate.cache.spi.entry.CacheEntry)2 Contact (org.hibernate.test.cache.infinispan.functional.entities.Contact)2 Item (org.hibernate.test.domain.Item)2 NotificationFilter (com.thoughtworks.go.domain.NotificationFilter)1