Search in sources :

Example 76 with Statistics

use of org.hibernate.stat.Statistics in project micrometer by micrometer-metrics.

the class HibernateMetricsTest method createEntityManagerFactoryMock.

private static EntityManagerFactory createEntityManagerFactoryMock(final boolean statsEnabled) {
    EntityManagerFactory emf = Mockito.mock(EntityManagerFactory.class);
    SessionFactory sf = Mockito.mock(SessionFactory.class);
    Statistics stats = Mockito.mock(Statistics.class, invocation -> {
        if (invocation.getMethod().getName().equals("isStatisticsEnabled")) {
            return statsEnabled;
        }
        return 42L;
    });
    when(emf.unwrap(SessionFactory.class)).thenReturn(sf);
    when(sf.getStatistics()).thenReturn(stats);
    return emf;
}
Also used : SessionFactory(org.hibernate.SessionFactory) EntityManagerFactory(javax.persistence.EntityManagerFactory) Statistics(org.hibernate.stat.Statistics)

Example 77 with Statistics

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

the class DatabaseWebService method getHibernateStatistics.

private HibernateStatisticsVO getHibernateStatistics() {
    Statistics statistics = DBFactory.getInstance().getStatistics();
    if (!statistics.isStatisticsEnabled()) {
        return null;
    }
    HibernateStatisticsVO stats = new HibernateStatisticsVO();
    stats.setOpenSessionsCount(statistics.getSessionOpenCount());
    stats.setTransactionsCount(statistics.getTransactionCount());
    stats.setSuccessfulTransactionCount(statistics.getSuccessfulTransactionCount());
    stats.setFailedTransactionsCount(statistics.getTransactionCount() - statistics.getSuccessfulTransactionCount());
    stats.setOptimisticFailureCount(statistics.getOptimisticFailureCount());
    stats.setQueryExecutionCount(statistics.getQueryExecutionCount());
    stats.setQueryExecutionMaxTime(statistics.getQueryExecutionMaxTime());
    stats.setQueryExecutionMaxTimeQueryString(statistics.getQueryExecutionMaxTimeQueryString());
    return stats;
}
Also used : HibernateStatisticsVO(org.olat.restapi.system.vo.HibernateStatisticsVO) Statistics(org.hibernate.stat.Statistics)

Example 78 with Statistics

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

the class HibernateEntitiesController method loadModel.

public void loadModel() {
    Statistics statistics = dbInstance.getStatistics();
    String[] entities = statistics.getEntityNames();
    List<QueryInfos> infos = new ArrayList<>(entities.length);
    for (String entity : entities) {
        EntityStatistics queryStats = statistics.getEntityStatistics(entity);
        infos.add(new QueryInfos(entity, queryStats));
    }
    tableModel.setObjects(infos);
    table.reset();
}
Also used : ArrayList(java.util.ArrayList) EntityStatistics(org.hibernate.stat.EntityStatistics) Statistics(org.hibernate.stat.Statistics) EntityStatistics(org.hibernate.stat.EntityStatistics)

Example 79 with Statistics

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

the class HibernateQueriesController method loadModel.

public void loadModel() {
    Statistics statistics = dbInstance.getStatistics();
    String[] queries = statistics.getQueries();
    List<QueryInfos> infos = new ArrayList<>(queries.length);
    for (String query : queries) {
        QueryStatistics queryStats = statistics.getQueryStatistics(query);
        infos.add(new QueryInfos(query, queryStats));
    }
    tableModel.setObjects(infos);
    table.reset();
}
Also used : QueryStatistics(org.hibernate.stat.QueryStatistics) ArrayList(java.util.ArrayList) QueryStatistics(org.hibernate.stat.QueryStatistics) Statistics(org.hibernate.stat.Statistics)

Example 80 with Statistics

use of org.hibernate.stat.Statistics in project microservices by pwillhan.

the class SecondLevel method cacheQueryResults.

@Test
public void cacheQueryResults() throws Exception {
    CacheTestData testData = storeTestData();
    Long USER_ID = testData.users.getFirstId();
    Long ITEM_ID = testData.items.getFirstId();
    UserTransaction tx = TM.getUserTransaction();
    try {
        // Clear the Item entity cache region
        JPA.getEntityManagerFactory().getCache().evict(Item.class);
        Statistics stats = JPA.getEntityManagerFactory().unwrap(SessionFactory.class).getStatistics();
        {
            tx.begin();
            EntityManager em = JPA.createEntityManager();
            String queryString = "select i from Item i where i.name like :n";
            /* 
                   You have to enable caching for a particular query. Without
                   the <code>org.hibernate.cachable</code> hint, the
                   result won't be stored in the query result cache.
                 */
            Query query = em.createQuery(queryString).setParameter("n", "I%").setHint("org.hibernate.cacheable", true);
            /* 
                   Hibernate will now execute the SQL query and retrieve the
                   result set into memory.                 */
            List<Item> items = query.getResultList();
            assertEquals(items.size(), 3);
            /* 
                   Using the statistics API, you can find out more details.
                   This is the first time you execute this query, so you get
                   a cache miss, not a hit. Hibernate puts the query and
                   its result into the cache. If you run the exact same query
                   again, the result will be from the cache.
                 */
            QueryStatistics queryStats = stats.getQueryStatistics(queryString);
            assertEquals(queryStats.getCacheHitCount(), 0);
            assertEquals(queryStats.getCacheMissCount(), 1);
            assertEquals(queryStats.getCachePutCount(), 1);
            /* 
                   The actual entity instance data retrieved in the result set is
                   stored in the entity cache region, not in the query result cache.
                 */
            SecondLevelCacheStatistics itemCacheStats = stats.getSecondLevelCacheStatistics(Item.class.getName());
            assertEquals(itemCacheStats.getElementCountInMemory(), 3);
            tx.commit();
            em.close();
        }
        {
            // Execute the query again, hitting the cache
            tx.begin();
            EntityManager em = JPA.createEntityManager();
            String queryString = "select i from Item i where i.name like :n";
            List<Item> items = em.createQuery(queryString).setParameter("n", "I%").setHint("org.hibernate.cacheable", true).getResultList();
            assertEquals(items.size(), 3);
            QueryStatistics queryStats = stats.getQueryStatistics(queryString);
            assertEquals(queryStats.getCacheHitCount(), 1);
            assertEquals(queryStats.getCacheMissCount(), 1);
            assertEquals(queryStats.getCachePutCount(), 1);
            tx.commit();
            em.close();
        }
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) SessionFactory(org.hibernate.SessionFactory) EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) QueryStatistics(org.hibernate.stat.QueryStatistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) List(java.util.List) Statistics(org.hibernate.stat.Statistics) QueryStatistics(org.hibernate.stat.QueryStatistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) NaturalIdCacheStatistics(org.hibernate.stat.NaturalIdCacheStatistics) Test(org.testng.annotations.Test) JPATest(org.jpwh.env.JPATest)

Aggregations

Statistics (org.hibernate.stat.Statistics)97 Test (org.junit.Test)59 Session (org.hibernate.Session)27 SecondLevelCacheStatistics (org.hibernate.stat.SecondLevelCacheStatistics)26 EntityManager (javax.persistence.EntityManager)21 QueryStatistics (org.hibernate.stat.QueryStatistics)19 Item (org.hibernate.test.cache.infinispan.functional.entities.Item)14 Transaction (org.hibernate.Transaction)12 ArrayList (java.util.ArrayList)11 VersionedItem (org.hibernate.test.cache.infinispan.functional.entities.VersionedItem)11 List (java.util.List)10 OtherItem (org.hibernate.test.cache.infinispan.functional.entities.OtherItem)10 SessionFactory (org.hibernate.SessionFactory)9 CacheRegionStatistics (org.hibernate.stat.CacheRegionStatistics)9 ByRef (org.infinispan.commons.util.ByRef)8 TestForIssue (org.hibernate.testing.TestForIssue)6 Date (java.util.Date)5 UserTransaction (javax.transaction.UserTransaction)5 NaturalIdCacheStatistics (org.hibernate.stat.NaturalIdCacheStatistics)5 Test (org.testng.annotations.Test)5