Spring引导中的Hibernate统计不起作用?

Jim*_*imB 15 java statistics spring hibernate spring-boot

我这里有一个小样本应用程序

https://github.com/jimbasilio/SpringBoot
Run Code Online (Sandbox Code Playgroud)

这会在数据库中创建一些简单的数据以及我正在努力的其他一些东西,以便在春季启动时抛出轮胎(旁注:到目前为止,我喜欢弹簧启动!).如果你克隆git repo,你可以访问网址:

http://127.0.0.1:8080/hello/get/1
Run Code Online (Sandbox Code Playgroud)

它将从数据库加载并将hibernate统计信息写入控制台.

我有一个问题,无论是否通过application.properties文件配置hibernate统计信息:

hibernate.generate_statistics=true
Run Code Online (Sandbox Code Playgroud)

当我写出hibernate统计数据时,我没有任何用处.我通过以下方式获取统计数据:

Session session = (Session) this.entityManager.getDelegate();
session.getSessionFactory().getStatistics().logSummary();
HelloEntity entity = helloRepository.findOne(id);
entityManager.flush();
session.getSessionFactory().getStatistics().logSummary();
Run Code Online (Sandbox Code Playgroud)

我的第二条日志消息(刷新​​后)如下.你可以看到它甚至没有注册被打开的会话.我正在使用spring boot 1.0.1.RELEASE.

的pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.0.1.RELEASE</version>
</parent>
Run Code Online (Sandbox Code Playgroud)

日志文件:

2014-04-28 20:51:29.415  INFO 18044 --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000161: Logging statistics....
2014-04-28 20:51:29.416  INFO 18044 --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000251: Start time: 1398732682476
2014-04-28 20:51:29.416  INFO 18044 --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000242: Sessions opened: 0
2014-04-28 20:51:29.416  INFO 18044 --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000241: Sessions closed: 0
2014-04-28 20:51:29.416  INFO 18044 --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000266: Transactions: 0
2014-04-28 20:51:29.416  INFO 18044 --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000258: Successful transactions: 0
2014-04-28 20:51:29.417  INFO 18044 --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000187: Optimistic lock failures: 0
2014-04-28 20:51:29.417  INFO 18044 --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000105: Flushes: 0
2014-04-28 20:51:29.417  INFO 18044 --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000048: Connections obtained: 0
2014-04-28 20:51:29.417  INFO 18044 --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000253: Statements prepared: 0
2014-04-28 20:51:29.417  INFO 18044 --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000252: Statements closed: 0
2014-04-28 20:51:29.417  INFO 18044 --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000239: Second level cache puts: 0
2014-04-28 20:51:29.418  INFO 18044 --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000237: Second level cache hits: 0
2014-04-28 20:51:29.418  INFO 18044 --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000238: Second level cache misses: 0
2014-04-28 20:51:29.418  INFO 18044 --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000079: Entities loaded: 0
2014-04-28 20:51:29.418  INFO 18044 --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000080: Entities updated: 0
2014-04-28 20:51:29.418  INFO 18044 --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000078: Entities inserted: 0
2014-04-28 20:51:29.418  INFO 18044 --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000076: Entities deleted: 0
2014-04-28 20:51:29.419  INFO 18044 --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000077: Entities fetched (minimize this): 0
2014-04-28 20:51:29.419  INFO 18044 --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000033: Collections loaded: 0
2014-04-28 20:51:29.419  INFO 18044 --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000036: Collections updated: 0
2014-04-28 20:51:29.419  INFO 18044 --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000035: Collections removed: 0
2014-04-28 20:51:29.419  INFO 18044 --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000034: Collections recreated: 0
2014-04-28 20:51:29.420  INFO 18044 --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000032: Collections fetched (minimize this): 0
2014-04-28 20:51:29.420  INFO 18044 --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000438: NaturalId cache puts: 0
2014-04-28 20:51:29.420  INFO 18044 --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000439: NaturalId cache hits: 0
2014-04-28 20:51:29.420  INFO 18044 --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000440: NaturalId cache misses: 0
2014-04-28 20:51:29.420  INFO 18044 --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000441: Max NaturalId query time: 0ms
2014-04-28 20:51:29.420  INFO 18044 --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000442: NaturalId queries executed to database: 0
2014-04-28 20:51:29.420  INFO 18044 --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000210: Queries executed to database: 0
2014-04-28 20:51:29.420  INFO 18044 --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000215: Query cache puts: 0
2014-04-28 20:51:29.421  INFO 18044 --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000433: update timestamps cache puts: 0
2014-04-28 20:51:29.421  INFO 18044 --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000434: update timestamps cache hits: 0
2014-04-28 20:51:29.421  INFO 18044 --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000435: update timestamps cache misses: 0
2014-04-28 20:51:29.421  INFO 18044 --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000213: Query cache hits: 0
2014-04-28 20:51:29.421  INFO 18044 --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000214: Query cache misses: 0
2014-04-28 20:51:29.421  INFO 18044 --- [nio-8080-exec-1] o.h.s.internal.ConcurrentStatisticsImpl  : HHH000173: Max query time: 0ms
Run Code Online (Sandbox Code Playgroud)

小智 57

使用 spring.jpa.properties.hibernate.generate_statistics=true 代替
hibernate.generate_statistics=true

  • 另外,请确保为休眠启用了日志记录。例如:“logging.level.org.hibernate=INFO” (4认同)

ger*_*tan 2

我认为 Hibernate 只是不读取您的配置。

根据Hibernate 的参考手册,您应该将 Hibernate 配置放在 hibernate.properties 或 hibernate.cfg.xml 上。如果这些不起作用,您可能还想尝试编程配置。

  • 事实证明,您可以为本机 JPA 提供程序添加前缀设置并将它们保留在 application.properties 中,我在 Spring Boot 文档中错过了这一点。所以在我的例子中,我添加了: spring.jpa.properties.hibernate.generate_statistics=true 到我的 application.properties 文件中,一切也正常。与 hibernate.properties 文件相比,我更喜欢这个文件,因为我可以使用 application-{profile}.properties 覆盖 dev/test/prod 设置。再次感谢你的踢球,它很有帮助。 (8认同)