Hibernate何时打印统计信息?

au9on6nz  于 8个月前  发布在  其他
关注(0)|答案(6)|浏览(71)

我最近将hibernate集成到我的webapp中,并试图查看正在发生的db调用的性能影响/频率。
在启用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之外,很少看到任何其他指标的非零值。
那么记录器到底什么时候运行,我做错了什么才能得到这么多度量日志?
谢谢

z0qdvdin

z0qdvdin1#

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

log4j.logger.org.hibernate.engine.internal.StatisticalLoggingSessionEventListener=OFF
noj0wjuj

noj0wjuj2#

如果你不想深入到会话事件,你可以使用这个属性“hibernate.session.events.log=false"来禁止它们被记录。(根据需要,您也可以使用log4j配置来执行此操作)。
看起来,从Hibernate 4开始,如果您启用“hibernate.generate_statistics",则默认情况下也会记录会话事件。但是,由于每个会话都记录了事件,因此日志将被大量填满。因此,在需要本地分析任何性能问题时使用。
有关启用/禁用事件日志的更多详细信息,请单击此处:https://hibernate.atlassian.net/browse/HHH-8793

bnl4lu3b

bnl4lu3b3#

它帮助了我:

spring:
  jpa:
    properties:
      generate_statistics: false

在application.yml中

lnxxn5zx

lnxxn5zx4#

对于Quarkus,相关的application.properties条目是:

quarkus.hibernate-orm.metrics.enabled = false
n8ghc7c1

n8ghc7c15#

我禁用它通过使用删除或禁用下面的属性-
spring.jpa.properties.hibernate.generate_statistics = false

ct2axkht

ct2axkht6#

我们可以通过在logback.xml或log4j.xml文件中添加一个属性来专门为这个类禁用日志记录:

<logger name="org.hibernate.engine.internal.StatisticalLoggingSessionEventListener" level="OFF"/>

相关问题