春季启动中的休眠统计信息不起作用?
Posted
技术标签:
【中文标题】春季启动中的休眠统计信息不起作用?【英文标题】:Hibernate statistics in spring boot not working? 【发布时间】:2014-06-14 18:00:03 【问题描述】:我在这里有一个小示例应用程序
https://github.com/jimbasilio/SpringBoot
这会在数据库中创建一些简单的数据,以及我正在做的一些其他事情,以便在弹簧靴上踢轮胎(旁注:到目前为止,我喜欢弹簧靴!!)。如果你克隆 git repo,你可以访问 url:
http://127.0.0.1:8080/hello/get/1
它会从数据库加载并将休眠统计信息写入控制台。
我确实有一个问题,无论是否通过 application.properties 文件配置休眠统计信息:
hibernate.generate_statistics=true
当我写出休眠统计信息时,我没有得到任何有用的信息。我正在通过以下方式获取统计信息:
Session session = (Session) this.entityManager.getDelegate();
session.getSessionFactory().getStatistics().logSummary();
HelloEntity entity = helloRepository.findOne(id);
entityManager.flush();
session.getSessionFactory().getStatistics().logSummary();
我的第二条日志消息(刷新后)如下。您可以看到它甚至没有注册正在打开的会话。我正在使用 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>
日志文件:
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
【问题讨论】:
【参考方案1】:我认为 Hibernate 只是没有读取您的配置。
根据Hibernate's Reference Manual,您应该将 Hibernate 配置放在 hibernate.properties 或 hibernate.cfg.xml 上。如果这些都不起作用,您可能还想尝试编程配置。
【讨论】:
就是这样!!我有点震惊。我以为所有的属性都合并在一起了。谢谢你的回答! 原来您可以为本地 JPA 提供程序添加前缀设置并将它们保存在 application.properties 中,我在 Spring Boot 文档中错过了这一点。所以在我的情况下,我在 application.properties 文件中添加了 spring.jpa.properties.hibernate.generate_statistics=true 并且事情也有效。我更喜欢这个 hibernate.properties 文件,因为我可以用 application-profile.properties 覆盖 dev/test/prod 设置。再次感谢您的帮助,它有帮助。【参考方案2】:使用spring.jpa.properties.hibernate.generate_statistics=true
代替hibernate.generate_statistics=true
【讨论】:
这是正确的方式,我的意思是“弹簧靴”的方式。 另外确保为休眠启用日志记录。例如:logging.level.org.hibernate=INFO
以上是关于春季启动中的休眠统计信息不起作用?的主要内容,如果未能解决你的问题,请参考以下文章