Search in sources :

Example 1 with QueryStatistics

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

the class QueryCacheTest method testQueryCacheFetch.

@Test
public void testQueryCacheFetch() throws Exception {
    sessionFactory().getCache().evictQueryRegions();
    sessionFactory().getStatistics().clear();
    // persist our 2 items.  This saves them to the db, but also into the second level entity cache region
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Item i = new Item();
    i.setName("widget");
    i.setDescription("A really top-quality, full-featured widget.");
    Item i2 = new Item();
    i2.setName("other widget");
    i2.setDescription("Another decent widget.");
    s.persist(i);
    s.persist(i2);
    t.commit();
    s.close();
    final String queryString = "from Item i where i.name like '%widget'";
    QueryStatistics qs = s.getSessionFactory().getStatistics().getQueryStatistics(queryString);
    Thread.sleep(200);
    // perform the cacheable query.  this will execute the query (no query cache hit), but the Items will be
    // found in second level entity cache region
    s = openSession();
    t = s.beginTransaction();
    List result = s.createQuery(queryString).setCacheable(true).list();
    assertEquals(result.size(), 2);
    t.commit();
    s.close();
    assertEquals(qs.getCacheHitCount(), 0);
    assertEquals(s.getSessionFactory().getStatistics().getEntityFetchCount(), 0);
    // evict the Items from the second level entity cache region
    sessionFactory().getCache().evictEntityRegion(Item.class);
    // now, perform the cacheable query again.  this time we should not execute the query (query cache hit).
    // However, the Items will not be found in second level entity cache region this time (we evicted them above)
    // nor are they in associated with the session.
    s = openSession();
    t = s.beginTransaction();
    result = s.createQuery(queryString).setCacheable(true).list();
    assertEquals(result.size(), 2);
    assertTrue(Hibernate.isInitialized(result.get(0)));
    assertTrue(Hibernate.isInitialized(result.get(1)));
    t.commit();
    s.close();
    assertEquals(qs.getCacheHitCount(), 1);
    assertEquals(s.getSessionFactory().getStatistics().getEntityFetchCount(), 1);
    s = openSession();
    t = s.beginTransaction();
    s.createQuery("delete Item").executeUpdate();
    t.commit();
    s.close();
}
Also used : Transaction(org.hibernate.Transaction) QueryStatistics(org.hibernate.stat.QueryStatistics) ArrayList(java.util.ArrayList) List(java.util.List) Session(org.hibernate.Session) Test(org.junit.Test)

Example 2 with QueryStatistics

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

the class QueryCacheTest method testHitCacheInSameSession.

@Test
@TestForIssue(jiraKey = "JBPAPP-4224")
public void testHitCacheInSameSession() {
    sessionFactory().getCache().evictQueryRegions();
    sessionFactory().getStatistics().clear();
    Session s = openSession();
    List list = new ArrayList();
    s.beginTransaction();
    for (int i = 0; i < 3; i++) {
        Item a = new Item();
        a.setName("a" + i);
        a.setDescription("a" + i);
        list.add(a);
        s.persist(a);
    }
    s.getTransaction().commit();
    // s.close();
    // s=openSession();
    s.beginTransaction();
    String queryString = "from Item";
    // this query will hit the database and create the cache
    s.createQuery(queryString).setCacheable(true).list();
    s.getTransaction().commit();
    s.beginTransaction();
    // and this one SHOULD served by the cache
    s.createQuery(queryString).setCacheable(true).list();
    s.getTransaction().commit();
    QueryStatistics qs = s.getSessionFactory().getStatistics().getQueryStatistics(queryString);
    assertEquals(1, qs.getCacheHitCount());
    assertEquals(1, qs.getCachePutCount());
    s.close();
    s = openSession();
    s.beginTransaction();
    for (Object obj : list) {
        s.delete(obj);
    }
    s.getTransaction().commit();
    s.close();
}
Also used : QueryStatistics(org.hibernate.stat.QueryStatistics) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 3 with QueryStatistics

use of org.hibernate.stat.QueryStatistics in project openolat by klemens.

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 4 with QueryStatistics

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

Example 5 with QueryStatistics

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

Aggregations

QueryStatistics (org.hibernate.stat.QueryStatistics)16 Statistics (org.hibernate.stat.Statistics)9 List (java.util.List)8 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)7 Session (org.hibernate.Session)6 Transaction (org.hibernate.Transaction)5 EntityManager (javax.persistence.EntityManager)4 Query (javax.persistence.Query)4 CacheRegionStatistics (org.hibernate.stat.CacheRegionStatistics)3 Date (java.util.Date)2 Iterator (java.util.Iterator)2 Map (java.util.Map)2 ScrollableResults (org.hibernate.ScrollableResults)2 SessionFactory (org.hibernate.SessionFactory)2 EntityStatistics (org.hibernate.stat.EntityStatistics)2 SecondLevelCacheStatistics (org.hibernate.stat.SecondLevelCacheStatistics)2 HashMap (java.util.HashMap)1 UserTransaction (javax.transaction.UserTransaction)1 StatsEntry (org.bedework.calfacade.BwStats.StatsEntry)1