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