Search in sources :

Example 31 with Statistics

use of org.hibernate.stat.Statistics 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 32 with Statistics

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

the class SFSB2LC method queryCacheCheckIfEmpty.

/**
     * Checking if query cache is empty
     *
     * @param id Employee's id in the query
     */
public String queryCacheCheckIfEmpty(String id) {
    EntityManager em = emf.createEntityManager();
    Statistics stats = em.unwrap(Session.class).getSessionFactory().getStatistics();
    stats.clear();
    try {
        // the nextTimestamp from infinispan is "return System.currentTimeMillis() / 100;"
        Thread.sleep(1000);
        String queryString = "from Employee e where e.id > " + id;
        QueryStatistics queryStats = stats.getQueryStatistics(queryString);
        Query query = em.createQuery(queryString);
        query.setHint("org.hibernate.cacheable", true);
        // query - this call shouldn't hit the cache -> query cache is empty
        query.getResultList();
        assertEquals("Expected 1 miss in cache" + generateQueryCacheStats(queryStats), 1, queryStats.getCacheMissCount());
        assertEquals("Expected 1 put in cache" + generateQueryCacheStats(queryStats), 1, queryStats.getCachePutCount());
        assertEquals("Expected no hits in cache" + generateQueryCacheStats(queryStats), 0, queryStats.getCacheHitCount());
    } catch (AssertionError e) {
        return e.getMessage();
    } catch (InterruptedException e) {
        return e.getMessage();
    } finally {
        em.close();
    }
    return "OK";
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) QueryStatistics(org.hibernate.stat.QueryStatistics) QueryStatistics(org.hibernate.stat.QueryStatistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Statistics(org.hibernate.stat.Statistics)

Example 33 with Statistics

use of org.hibernate.stat.Statistics 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 34 with Statistics

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

the class SFSB2LC method queryCacheCheck.

/**
     * Performs 2 query calls, first call put query in the cache and second should hit the cache
     *
     * @param id Employee's id in the query
     */
public String queryCacheCheck(String id) {
    // the nextTimestamp from infinispan is "return System.currentTimeMillis()"
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
        return e.getMessage();
    }
    EntityManager em = emf.createEntityManager();
    Statistics stats = em.unwrap(Session.class).getSessionFactory().getStatistics();
    stats.clear();
    try {
        String queryString = "from Employee e where e.id > " + id;
        QueryStatistics queryStats = stats.getQueryStatistics(queryString);
        Query query = em.createQuery(queryString);
        query.setHint("org.hibernate.cacheable", true);
        // query - this call should fill the cache
        query.getResultList();
        assertEquals("Expected 1 miss in cache" + generateQueryCacheStats(queryStats), 1, queryStats.getCacheMissCount());
        assertEquals("Expected 1 put in cache" + generateQueryCacheStats(queryStats), 1, queryStats.getCachePutCount());
        assertEquals("Expected no hits in cache" + generateQueryCacheStats(queryStats), 0, queryStats.getCacheHitCount());
        // query - second call should hit cache
        query.getResultList();
        assertEquals("Expected 1 hit in cache" + generateQueryCacheStats(queryStats), 1, queryStats.getCacheHitCount());
    } catch (AssertionError e) {
        return e.getMessage();
    } finally {
        em.close();
    }
    return "OK";
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) QueryStatistics(org.hibernate.stat.QueryStatistics) QueryStatistics(org.hibernate.stat.QueryStatistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Statistics(org.hibernate.stat.Statistics)

Example 35 with Statistics

use of org.hibernate.stat.Statistics 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)

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