Search in sources :

Example 16 with Statistics

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

the class HibernateInformationProvider method asJson.

@Override
public Map<String, Object> asJson() {
    LinkedHashMap<String, Object> json = new LinkedHashMap<>();
    Statistics statistics = sessionFactory.getStatistics();
    if (!statistics.isStatisticsEnabled()) {
        return json;
    }
    json.put("EntityDeleteCount", statistics.getEntityDeleteCount());
    json.put("EntityInsertCount", statistics.getEntityInsertCount());
    json.put("EntityLoadCount", statistics.getEntityLoadCount());
    json.put("EntityFetchCount", statistics.getEntityFetchCount());
    json.put("EntityUpdateCount", statistics.getEntityUpdateCount());
    json.put("QueryExecutionCount", statistics.getQueryExecutionCount());
    json.put("QueryExecutionMaxTime", statistics.getQueryExecutionMaxTime());
    json.put("QueryExecutionMaxTimeQueryString", statistics.getQueryExecutionMaxTimeQueryString());
    json.put("QueryCacheHitCount", statistics.getQueryCacheHitCount());
    json.put("QueryCacheMissCount", statistics.getQueryCacheMissCount());
    json.put("QueryCachePutCount", statistics.getQueryCachePutCount());
    json.put("FlushCount", statistics.getFlushCount());
    json.put("ConnectCount", statistics.getConnectCount());
    json.put("SecondLevelCacheHitCount", statistics.getSecondLevelCacheHitCount());
    json.put("SecondLevelCacheMissCount", statistics.getSecondLevelCacheMissCount());
    json.put("SecondLevelCachePutCount", statistics.getSecondLevelCachePutCount());
    json.put("SessionCloseCount", statistics.getSessionCloseCount());
    json.put("SessionOpenCount", statistics.getSessionOpenCount());
    json.put("CollectionLoadCount", statistics.getCollectionLoadCount());
    json.put("CollectionFetchCount", statistics.getCollectionFetchCount());
    json.put("CollectionUpdateCount", statistics.getCollectionUpdateCount());
    json.put("CollectionRemoveCount", statistics.getCollectionRemoveCount());
    json.put("CollectionRecreateCount", statistics.getCollectionRecreateCount());
    json.put("StartTime", statistics.getStartTime());
    json.put("SecondLevelCacheRegionNames", statistics.getSecondLevelCacheRegionNames());
    json.put("SuccessfulTransactionCount", statistics.getSuccessfulTransactionCount());
    json.put("TransactionCount", statistics.getTransactionCount());
    json.put("PrepareStatementCount", statistics.getPrepareStatementCount());
    json.put("CloseStatementCount", statistics.getCloseStatementCount());
    json.put("OptimisticFailureCount", statistics.getOptimisticFailureCount());
    LinkedHashMap<String, Object> queryStats = new LinkedHashMap<>();
    json.put("Queries", queryStats);
    String[] queries = statistics.getQueries();
    for (String query : queries) {
        queryStats.put(query, statistics.getQueryStatistics(query));
    }
    LinkedHashMap<String, Object> entityStatistics = new LinkedHashMap<>();
    json.put("EntityStatistics", entityStatistics);
    String[] entityNames = statistics.getEntityNames();
    for (String entityName : entityNames) {
        entityStatistics.put(entityName, statistics.getEntityStatistics(entityName));
    }
    LinkedHashMap<String, Object> roleStatistics = new LinkedHashMap<>();
    json.put("RoleStatistics", roleStatistics);
    String[] roleNames = statistics.getCollectionRoleNames();
    for (String roleName : roleNames) {
        roleStatistics.put(roleName, statistics.getCollectionStatistics(roleName));
    }
    return json;
}
Also used : Statistics(org.hibernate.stat.Statistics) LinkedHashMap(java.util.LinkedHashMap)

Example 17 with Statistics

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

the class SecondLevelCacheTest method testCache.

@Test
public void testCache() {
    doInJPA(this::entityManagerFactory, entityManager -> {
        entityManager.persist(new Person());
        entityManager.persist(new Person());
        Person aPerson = new Person();
        aPerson.setName("John Doe");
        aPerson.setCode("unique-code");
        entityManager.persist(aPerson);
        return aPerson;
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        log.info("Jpa load by id");
        Person person = entityManager.find(Person.class, 1L);
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        log.info("Native load by id");
        Session session = entityManager.unwrap(Session.class);
        Person person = session.get(Person.class, 1L);
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        log.info("Native load by natural-id");
        Session session = entityManager.unwrap(Session.class);
        Person person = session.byNaturalId(Person.class).using("code", "unique-code").load();
        assertNotNull(person);
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        log.info("Jpa query cache");
        List<Person> persons = entityManager.createQuery("select p " + "from Person p " + "where p.name = :name", Person.class).setParameter("name", "John Doe").setHint("org.hibernate.cacheable", "true").getResultList();
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        log.info("Native query cache");
        Session session = entityManager.unwrap(Session.class);
        List<Person> persons = session.createQuery("select p " + "from Person p " + "where p.name = :name").setParameter("name", "John Doe").setCacheable(true).list();
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        log.info("Jpa query cache region");
        List<Person> persons = entityManager.createQuery("select p " + "from Person p " + "where p.id > :id", Person.class).setParameter("id", 0L).setHint(QueryHints.HINT_CACHEABLE, "true").setHint(QueryHints.HINT_CACHE_REGION, "query.cache.person").getResultList();
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        log.info("Native query cache");
        Session session = entityManager.unwrap(Session.class);
        List<Person> persons = session.createQuery("select p " + "from Person p " + "where p.id > :id").setParameter("id", 0L).setCacheable(true).setCacheRegion("query.cache.person").list();
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        log.info("Jpa query cache store mode ");
        List<Person> persons = entityManager.createQuery("select p " + "from Person p " + "where p.id > :id", Person.class).setParameter("id", 0L).setHint(QueryHints.HINT_CACHEABLE, "true").setHint(QueryHints.HINT_CACHE_REGION, "query.cache.person").setHint("javax.persistence.cache.storeMode", CacheStoreMode.REFRESH).getResultList();
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        log.info("Native query cache store mode");
        Session session = entityManager.unwrap(Session.class);
        List<Person> persons = session.createQuery("select p " + "from Person p " + "where p.id > :id").setParameter("id", 0L).setCacheable(true).setCacheRegion("query.cache.person").setCacheMode(CacheMode.REFRESH).list();
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        Session session = entityManager.unwrap(Session.class);
        Statistics statistics = session.getSessionFactory().getStatistics();
        SecondLevelCacheStatistics secondLevelCacheStatistics = statistics.getSecondLevelCacheStatistics("query.cache.person");
        long hitCount = secondLevelCacheStatistics.getHitCount();
        long missCount = secondLevelCacheStatistics.getMissCount();
        double hitRatio = (double) hitCount / (hitCount + missCount);
        return hitRatio;
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        log.info("Native query cache store mode");
        Session session = entityManager.unwrap(Session.class);
        session.getSessionFactory().getCache().evictQueryRegion("query.cache.person");
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        Map<String, Object> hints = new HashMap<>();
        hints.put("javax.persistence.cache.retrieveMode ", CacheRetrieveMode.USE);
        hints.put("javax.persistence.cache.storeMode", CacheStoreMode.REFRESH);
        Person person = entityManager.find(Person.class, 1L, hints);
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        Session session = entityManager.unwrap(Session.class);
        session.setCacheMode(CacheMode.REFRESH);
        Person person = session.get(Person.class, 1L);
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        List<Person> persons = entityManager.createQuery("select p from Person p", Person.class).setHint(QueryHints.HINT_CACHEABLE, "true").setHint("javax.persistence.cache.retrieveMode ", CacheRetrieveMode.USE).setHint("javax.persistence.cache.storeMode", CacheStoreMode.REFRESH).getResultList();
        entityManager.getEntityManagerFactory().getCache().evict(Person.class);
    });
    doInJPA(this::entityManagerFactory, entityManager -> {
        Session session = entityManager.unwrap(Session.class);
        List<Person> persons = session.createQuery("select p from Person p").setCacheable(true).setCacheMode(CacheMode.REFRESH).list();
        session.getSessionFactory().getCache().evictQueryRegion("query.cache.person");
    });
}
Also used : HashMap(java.util.HashMap) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Statistics(org.hibernate.stat.Statistics) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Session(org.hibernate.Session) Test(org.junit.Test)

Example 18 with Statistics

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

the class EntityManagerTest method testConfiguration.

@Test
public void testConfiguration() throws Exception {
    Item item = new Item("Mouse", "Micro$oft mouse");
    Distributor res = new Distributor();
    res.setName("Bruce");
    item.setDistributors(new HashSet<Distributor>());
    item.getDistributors().add(res);
    Statistics stats = ((HibernateEntityManagerFactory) entityManagerFactory()).getSessionFactory().getStatistics();
    stats.clear();
    stats.setStatisticsEnabled(true);
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    em.persist(res);
    em.persist(item);
    assertTrue(em.contains(item));
    em.getTransaction().commit();
    em.close();
    assertEquals(1, stats.getSecondLevelCachePutCount());
    assertEquals(0, stats.getSecondLevelCacheHitCount());
    em = getOrCreateEntityManager();
    em.getTransaction().begin();
    Item second = em.find(Item.class, item.getName());
    assertEquals(1, second.getDistributors().size());
    assertEquals(1, stats.getSecondLevelCacheHitCount());
    em.getTransaction().commit();
    em.close();
    em = getOrCreateEntityManager();
    em.getTransaction().begin();
    second = em.find(Item.class, item.getName());
    assertEquals(1, second.getDistributors().size());
    assertEquals(3, stats.getSecondLevelCacheHitCount());
    em.remove(second);
    em.remove(second.getDistributors().iterator().next());
    em.getTransaction().commit();
    em.close();
    stats.clear();
    stats.setStatisticsEnabled(false);
}
Also used : EntityManager(javax.persistence.EntityManager) Statistics(org.hibernate.stat.Statistics) Test(org.junit.Test)

Example 19 with Statistics

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

the class QueryTest method testNativeQueryByEntity.

@Test
public void testNativeQueryByEntity() {
    Item item = new Item("Mouse", "Micro$oft mouse");
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    try {
        em.persist(item);
        assertTrue(em.contains(item));
        em.getTransaction().commit();
        Statistics stats = em.getEntityManagerFactory().unwrap(SessionFactoryImplementor.class).getStatistics();
        stats.clear();
        assertEquals(0, stats.getFlushCount());
        em.getTransaction().begin();
        item = (Item) em.createNativeQuery("select * from Item", Item.class).getSingleResult();
        assertEquals(1, stats.getFlushCount());
        assertNotNull(item);
        assertEquals("Micro$oft mouse", item.getDescr());
        em.remove(item);
        em.getTransaction().commit();
    } catch (Exception e) {
        if (em.getTransaction() != null && em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        throw e;
    } finally {
        em.close();
    }
}
Also used : Item(org.hibernate.jpa.test.Item) EntityManager(javax.persistence.EntityManager) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) Statistics(org.hibernate.stat.Statistics) NoResultException(javax.persistence.NoResultException) PersistenceException(javax.persistence.PersistenceException) Test(org.junit.Test)

Example 20 with Statistics

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

the class PackagedEntityManagerTest method testConfiguration.

@Test
public void testConfiguration() throws Exception {
    File testPackage = buildExplicitPar();
    addPackageToClasspath(testPackage);
    emf = Persistence.createEntityManagerFactory("manager1", new HashMap());
    Item item = new Item("Mouse", "Micro$oft mouse");
    Distributor res = new Distributor();
    res.setName("Bruce");
    item.setDistributors(new HashSet<Distributor>());
    item.getDistributors().add(res);
    Statistics stats = ((HibernateEntityManagerFactory) emf).getSessionFactory().getStatistics();
    stats.clear();
    stats.setStatisticsEnabled(true);
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    em.persist(res);
    em.persist(item);
    assertTrue(em.contains(item));
    em.getTransaction().commit();
    em.close();
    assertEquals(1, stats.getSecondLevelCachePutCount());
    assertEquals(0, stats.getSecondLevelCacheHitCount());
    em = emf.createEntityManager();
    em.getTransaction().begin();
    Item second = em.find(Item.class, item.getName());
    assertEquals(1, second.getDistributors().size());
    assertEquals(1, stats.getSecondLevelCacheHitCount());
    em.getTransaction().commit();
    em.close();
    em = emf.createEntityManager();
    em.getTransaction().begin();
    second = em.find(Item.class, item.getName());
    assertEquals(1, second.getDistributors().size());
    assertEquals(3, stats.getSecondLevelCacheHitCount());
    for (Distributor distro : second.getDistributors()) {
        em.remove(distro);
    }
    em.remove(second);
    em.getTransaction().commit();
    em.close();
    stats.clear();
    stats.setStatisticsEnabled(false);
    emf.close();
}
Also used : Item(org.hibernate.jpa.test.Item) EntityManager(javax.persistence.EntityManager) Distributor(org.hibernate.jpa.test.Distributor) HashMap(java.util.HashMap) File(java.io.File) Statistics(org.hibernate.stat.Statistics) 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