Search in sources :

Example 11 with SecondLevelCacheStatistics

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

the class SFSB2LC method evictedEntityCacheCheck.

/**
     * Checks if entity 2LC is empty.
     */
public String evictedEntityCacheCheck(String CACHE_REGION_NAME) {
    EntityManager em = emf.createEntityManager();
    Statistics stats = em.unwrap(Session.class).getSessionFactory().getStatistics();
    stats.clear();
    SecondLevelCacheStatistics emp2LCStats = stats.getSecondLevelCacheStatistics(CACHE_REGION_NAME + "Employee");
    try {
        assertEquals("Expected no entities stored in the cache" + emp2LCStats, 0, emp2LCStats.getElementCountInMemory());
        // loading entity stored in previous session, we are expecting miss in 2lc
        Employee emp = getEmployee(em, 20);
        assertNotNull("Employee returned", emp);
        assertEquals("Expected 1 miss in 2LC" + generateEntityCacheStats(emp2LCStats), 1, emp2LCStats.getMissCount());
    } catch (AssertionError e) {
        return e.getMessage();
    } finally {
        em.close();
    }
    return "OK";
}
Also used : EntityManager(javax.persistence.EntityManager) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) QueryStatistics(org.hibernate.stat.QueryStatistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Statistics(org.hibernate.stat.Statistics)

Example 12 with SecondLevelCacheStatistics

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

the class SFSB2LC method addEntitiesAndEvictAll.

/**
     * Insert 2 entities and put them into the 2LC and then evicts entity cache.
     */
public String addEntitiesAndEvictAll(String CACHE_REGION_NAME) {
    EntityManager em = emf.createEntityManager();
    Statistics stats = em.unwrap(Session.class).getSessionFactory().getStatistics();
    stats.clear();
    SecondLevelCacheStatistics emp2LCStats = stats.getSecondLevelCacheStatistics(CACHE_REGION_NAME + "Employee");
    try {
        createEmployee(em, "Jan", "Ostrava", 20);
        createEmployee(em, "Martin", "Brno", 30);
        assertEquals("There are 2 puts in the 2LC" + generateEntityCacheStats(emp2LCStats), 2, emp2LCStats.getPutCount());
        assertTrue("Expected entities stored in the cache" + generateEntityCacheStats(emp2LCStats), emp2LCStats.getElementCountInMemory() > 0);
        // evict entity 2lc
        emf.getCache().evictAll();
    } catch (AssertionError e) {
        return e.getMessage();
    } finally {
        em.close();
    }
    return "OK";
}
Also used : EntityManager(javax.persistence.EntityManager) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) QueryStatistics(org.hibernate.stat.QueryStatistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Statistics(org.hibernate.stat.Statistics)

Example 13 with SecondLevelCacheStatistics

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

the class SFSB2LC method secondSessionCheck.

/**
     * Checking entity 2LC in a different EntityManager session
     */
public String secondSessionCheck(String CACHE_REGION_NAME) {
    EntityManager em = emf.createEntityManager();
    Statistics stats = em.unwrap(Session.class).getSessionFactory().getStatistics();
    stats.clear();
    SecondLevelCacheStatistics emp2LCStats = stats.getSecondLevelCacheStatistics(CACHE_REGION_NAME + "Employee");
    try {
        // add new entity
        createEmployee(em, "David", "Praha", 10);
        assertEquals("There is 1 put in the 2LC" + generateEntityCacheStats(emp2LCStats), 1, emp2LCStats.getPutCount());
    } catch (AssertionError e) {
        return e.getMessage();
    } finally {
        em.close();
    }
    EntityManager em2 = emf.createEntityManager();
    try {
        // loading entity stored in previous session, we'are expecting hit in cache
        Employee emp = getEmployee(em2, 10);
        assertNotNull("Employee returned", emp);
        assertEquals("Expected 1 hit in 2LC" + generateEntityCacheStats(emp2LCStats), 1, emp2LCStats.getHitCount());
    } catch (AssertionError e) {
        return e.getMessage();
    } finally {
        em2.close();
    }
    return "OK";
}
Also used : EntityManager(javax.persistence.EntityManager) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) QueryStatistics(org.hibernate.stat.QueryStatistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Statistics(org.hibernate.stat.Statistics)

Example 14 with SecondLevelCacheStatistics

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

the class StatefulBean method logStats.

private void logStats(String methodName) {
    Session session = em.unwrap(Session.class);
    log.trace(methodName + "(version=" + version + ", HashMap version=" + valueBag.get("version") + ") logging statistics for session = " + session);
    session.getSessionFactory().getStatistics().setStatisticsEnabled(true);
    session.getSessionFactory().getStatistics().logSummary();
    String[] entityRegionNames = session.getSessionFactory().getStatistics().getSecondLevelCacheRegionNames();
    for (String name : entityRegionNames) {
        log.trace("cache entity region name = " + name);
        SecondLevelCacheStatistics stats = session.getSessionFactory().getStatistics().getSecondLevelCacheStatistics(name);
        log.trace("2lc for " + name + ": " + stats.toString());
    }
// we will want to return the SecondLevelCacheStatistics for Employee
}
Also used : SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Session(org.hibernate.Session)

Example 15 with SecondLevelCacheStatistics

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

the class UserSqlMapDaoCachingTest method shouldCacheUserOnFind.

@Test
public void shouldCacheUserOnFind() {
    User first = new User("first");
    first.addNotificationFilter(new NotificationFilter("pipline", "stage1", StageEvent.Fails, true));
    first.addNotificationFilter(new NotificationFilter("pipline", "stage2", StageEvent.Fails, true));
    int originalUserCacheSize = sessionFactory.getStatistics().getSecondLevelCacheStatistics(User.class.getCanonicalName()).getEntries().size();
    int originalNotificationsCacheSize = sessionFactory.getStatistics().getSecondLevelCacheStatistics(User.class.getCanonicalName() + ".notificationFilters").getEntries().size();
    userDao.saveOrUpdate(first);
    long userId = userDao.findUser("first").getId();
    assertThat(sessionFactory.getStatistics().getSecondLevelCacheStatistics(User.class.getCanonicalName()).getEntries().size(), is(originalUserCacheSize + 1));
    SecondLevelCacheStatistics notificationFilterCollectionCache = sessionFactory.getStatistics().getSecondLevelCacheStatistics(User.class.getCanonicalName() + ".notificationFilters");
    assertThat(notificationFilterCollectionCache.getEntries().size(), is(originalNotificationsCacheSize + 1));
    assertThat(notificationFilterCollectionCache.getEntries().get(userId), is(Matchers.notNullValue()));
}
Also used : User(com.thoughtworks.go.domain.User) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) NotificationFilter(com.thoughtworks.go.domain.NotificationFilter) Test(org.junit.Test)

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