2017-06-12 115 views
2

我最近将hibernate集成到了我的webapp中,并试图查看正在发生的db调用的性能影响/频率。Hibernate何时打印统计信息?

启用show_sqlgenerate_statistics后,当我运行应用程序时,我看到由hibernate运行的SQL查询以及hibernate统计信息。例如:

08:04:53.724 [http-apr-8080-exec-1] INFO o.h.e.i.StatisticalLoggingSessionEventListener - Session Metrics { 
85648 nanoseconds spent acquiring 1 JDBC connections; 
0 nanoseconds spent releasing 0 JDBC connections; 
0 nanoseconds spent preparing 0 JDBC statements; 
0 nanoseconds spent executing 0 JDBC statements; 
0 nanoseconds spent executing 0 JDBC batches; 
0 nanoseconds spent performing 0 L2C puts; 
0 nanoseconds spent performing 0 L2C hits; 
0 nanoseconds spent performing 0 L2C misses; 
0 nanoseconds spent executing 0 flushes (flushing a total of 0 entities and 0 collections); 
0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections) 
} 

但是,统计语句每次api调用都会打印多次,而查询语句的数量要少得多。另外,对于统计日志中除acquiring 1 JDBC connections之外的任何其他度量标准,都非常罕见。

所以,当记录器正好运行,我做错了什么来获得这么多的公制日志?

感谢

回答

0

当您启用休眠统计你的会话统计信息每一个会话关闭时间。 如果你不希望这种行为,你可以在你的log4j文件中添加以下条目的禁用它:

log4j.logger.org.hibernate.engine.internal.StatisticalLoggingSessionEventListener=OFF 
+0

所以应该不是每个各项统计日志也有一套相应的所以在该会话中运行的查询/报表(这将打印到控制台)?为什么在大多数情况下统计日志只是通过“获取JDBC连接”来打印? – sudshekhar

+0

实际上,为了打印统计信息,您必须从sessionFactory中检索统计信息对象。 统计信息stats = sessionFactory.getStatistics(); 然后您可以查询不同对象的统计信息:会话,实体,集合,缓存。 有关更多详细信息,请参阅[链接](https://docs.jboss.org/hibernate/orm/3.2/api/org/hibernate/stat/Statistics.html)。 –

+0

除了默认会话度量标准外,您还可以记录所需的每个度量标准,如果您希望某个特定实体上的度量标准可以调用,例如: 统计信息stats = sessionFactory.getStatistics(); EntityStatistics es = stats.getEntityStatistics(entityName); System.out.println(“Delete:”+ es.getDeleteCount()); –