Hibernate什么时候打印统计信息?

sud*_*har 9 java hibernate

我最近将休眠模式集成到了我的Web应用程序中,并试图查看正在发生的数据库调用对性能的影响/频率。

启用show_sql和之后generate_statistics,当我运行应用程序时,我会看到由hibernate运行的sql查询以及休眠统计信息。例如:

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)
}
Run Code Online (Sandbox Code Playgroud)

但是,每个api调用都会多次打印统计信息语句,而查询语句的数量要少得多。另外,很少看到acquiring 1 JDBC connections统计信息日志中没有其他指标的非零值。

那么,记录器何时准确运行?我要怎么做才能获取这么多的度量标准日志?

谢谢

Aba*_*s A 8

启用休眠统计信息后,每次关闭会话时都会获得会话统计信息。如果您不想要这种行为,您可以通过在 log4j 文件中添加以下条目来禁用它:

log4j.logger.org.hibernate.engine.internal.StatisticalLoggingSessionEventListener=OFF
Run Code Online (Sandbox Code Playgroud)


Cha*_*ana 8

如果您不想深入到会话事件,您可以使用此属性“ hibernate.session.events.log=false ”禁用它们被记录。(您也可以根据您的需要使用您的 log4j 配置来执行此操作)。

似乎从 hibernate 4 开始,如果您启用“ hibernate.generate_statistics ”,则默认情况下也会记录会话事件。但是随着每个会话记录事件,日志将被大量填满。因此,当您需要在本地分析任何性能问题时使用。

有关在此处启用/禁用事件日志的更多详细信息:https : //hibernate.atlassian.net/browse/HHH-8793


小智 6

它帮助了我:

spring:
  jpa:
    properties:
      generate_statistics: false
Run Code Online (Sandbox Code Playgroud)

在 application.yml 中