下面列出了怎么用org.hibernate.stat.EntityStatistics的API类实例代码及写法,或者点击链接到github查看源代码。
protected void logEntities(StringBuilder builder, String lineSep, Statistics stats) {
builder.append("Important entities statistics: ").append(lineSep);
for (String entity : stats.getEntityNames()) {
EntityStatistics entityStats = stats.getEntityStatistics(entity);
if (entityStats.getInsertCount() > LIMIT || entityStats.getDeleteCount() > LIMIT || entityStats.getUpdateCount() > LIMIT || entityStats.getLoadCount() > LIMIT || entityStats.getFetchCount() > LIMIT) {
builder.append(entity).append(" - ")
.append("inserted: ").append(entityStats.getInsertCount())
.append(", updated: ").append(entityStats.getUpdateCount())
.append(", removed: ").append(entityStats.getDeleteCount())
.append(", loaded: ").append(entityStats.getLoadCount())
.append(", fetched: ").append(entityStats.getFetchCount())
.append(lineSep);
}
}
builder.append(lineSep);
}
/**
* @see StatisticsServiceMBean#getEntityStatistics(java.lang.String)
*/
public EntityStatistics getEntityStatistics(String entityName) {
return stats.getEntityStatistics(entityName);
}
public void testQueryCacheInvalidation() throws Exception {
getSessions().evictQueries();
getSessions().getStatistics().clear();
final String queryString = "from Item i where i.name='widget'";
Session s = openSession();
Transaction t = s.beginTransaction();
s.createQuery( queryString ).setCacheable(true).list();
Item i = new Item();
i.setName("widget");
i.setDescription("A really top-quality, full-featured widget.");
s.save(i);
t.commit();
s.close();
QueryStatistics qs = s.getSessionFactory().getStatistics().getQueryStatistics( queryString );
EntityStatistics es = s.getSessionFactory().getStatistics().getEntityStatistics( Item.class.getName() );
Thread.sleep(200);
s = openSession();
t = s.beginTransaction();
List result = s.createQuery( queryString ).setCacheable(true).list();
assertEquals( result.size(), 1 );
t.commit();
s.close();
assertEquals( qs.getCacheHitCount(), 0 );
s = openSession();
t = s.beginTransaction();
result = s.createQuery( queryString ).setCacheable(true).list();
assertEquals( result.size(), 1 );
t.commit();
s.close();
assertEquals( qs.getCacheHitCount(), 1 );
assertEquals( s.getSessionFactory().getStatistics().getEntityFetchCount(), 0 );
s = openSession();
t = s.beginTransaction();
result = s.createQuery( queryString ).setCacheable(true).list();
assertEquals( result.size(), 1 );
assertTrue( Hibernate.isInitialized( result.get(0) ) );
i = (Item) result.get(0);
i.setName("Widget");
t.commit();
s.close();
assertEquals( qs.getCacheHitCount(), 2 );
assertEquals( qs.getCacheMissCount(), 2 );
assertEquals( s.getSessionFactory().getStatistics().getEntityFetchCount(), 0 );
Thread.sleep(200);
s = openSession();
t = s.beginTransaction();
result = s.createQuery( queryString ).setCacheable(true).list();
if ( dialectIsCaseSensitive("i.name='widget' should not match on case sensitive database.") ) {
assertEquals( result.size(), 0 );
}
i = (Item) s.get( Item.class, new Long(i.getId()) );
assertEquals( i.getName(), "Widget" );
s.delete(i);
t.commit();
s.close();
assertEquals( qs.getCacheHitCount(), 2 );
assertEquals( qs.getCacheMissCount(), 3 );
assertEquals( qs.getCachePutCount(), 3 );
assertEquals( qs.getExecutionCount(), 3 );
assertEquals( es.getFetchCount(), 0 ); //check that it was being cached
}
/**
* Get all the Hibernate Entities statistics aggregated in a sorted Map
*
* @param statistics
* @return a sorted map containing all the Hibernate entities stats
*/
public static SortedMap<String, Map<String, HibernateMetric>> getSecondLevelCacheEntitiesStats(
Statistics statistics) {
SortedMap<String, Map<String, HibernateMetric>> entitiesStatMap = new TreeMap<String, Map<String, HibernateMetric>>();
if (statistics.isStatisticsEnabled()) {
String[] entityNames = statistics.getEntityNames();
Stream.of(entityNames).forEach(eN -> {
EntityStatistics entityStat = statistics.getEntityStatistics(eN);
SortedMap<String, HibernateMetric> entityStatMap = new TreeMap<String, HibernateMetric>();
// Entity cache stats
entityStatMap.put(
"cache.region.name",
createHibernateMetricItem(
"cacheRegionName",
"The name of the region where this data is cached.",
entityStat.getCacheRegionName()));
entityStatMap.put(
"cache.hit.count",
createHibernateMetricItem(
"cacheHitCount",
"The number of successful cache look-ups for this data from its configured cache region since the last Statistics clearing.",
entityStat.getCacheHitCount()));
entityStatMap.put(
"cache.miss.count",
createHibernateMetricItem(
"cacheMissCount",
"The number of unsuccessful cache look-ups for this data from its configured cache region since the last Statistics clearing.",
entityStat.getCacheMissCount()));
entityStatMap.put(
"cache.put.count",
createHibernateMetricItem(
"cachePutCount",
"The number of times this data has been into its configured cache region since the last Statistics clearing.",
entityStat.getCachePutCount()));
double hitsRatio = (entityStat.getCacheHitCount() + entityStat.getCacheMissCount()) != 0
? ((double) entityStat.getCacheHitCount()
/ (entityStat.getCacheHitCount() + entityStat.getCacheMissCount()) * 100)
: -1;
entityStatMap.put(
"cache.hit.ratio",
createHibernateMetricItem(
"cacheHitRatio",
"The ratio of successful cache look-ups for this data from its configured cache region since the last Statistics clearing.",
df2.format(hitsRatio)));
// Entity stats
entityStatMap.put(
"fetch.count",
createHibernateMetricItem(
"fetchCount",
"Number of times (since last Statistics clearing) this entity has been fetched.",
entityStat.getFetchCount()));
entityStatMap.put(
"insert.count",
createHibernateMetricItem(
"insertCount",
"Number of times (since last Statistics clearing) this entity has been inserted.",
entityStat.getInsertCount()));
entityStatMap.put(
"delete.count",
createHibernateMetricItem(
"deleteCount",
"Number of times (since last Statistics clearing) this entity has been deleted.",
entityStat.getDeleteCount()));
entityStatMap.put(
"load.count",
createHibernateMetricItem(
"loadCount",
"Number of times (since last Statistics clearing) this entity has been loaded.",
entityStat.getLoadCount()));
entityStatMap.put(
"optimistic.failure.count",
createHibernateMetricItem(
"optimisticFailureCount",
"Number of times (since last Statistics clearing) this entity has experienced an optimistic lock failure.",
entityStat.getOptimisticFailureCount()));
entityStatMap.put(
"update.count",
createHibernateMetricItem(
"updateCount",
"Number of times (since last Statistics clearing) this entity has been updated.",
entityStat.getUpdateCount()));
entitiesStatMap.put(ENTITY_STATS_PREFIX + eN, entityStatMap);
});
}
return entitiesStatMap;
}
public EntityStatistics getEntityStatistics(String entityName) {
return null;
}