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();
}
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();
}
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();
}
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";
}
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";
}
Aggregations