Java 如何访问 Hibernate 统计信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/643055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get access to the Hibernate Statistics
提问by mainstringargs
So in my persistence.xml
I turned on hibernate.generate_statistics
.
所以在我的persistence.xml
我打开了hibernate.generate_statistics
。
<property name="hibernate.generate_statistics">true</property>
My question is how do I access them? Where do the statistics go?
我的问题是如何访问它们?统计数据去哪儿了?
采纳答案by serg
In your dao service you can go:
在您的 dao 服务中,您可以:
Session session = this.sessionFactory.getCurrentSession();
SessionStatistics sessionStats = session.getStatistics();
Statistics stats = this.sessionFactory.getStatistics();
回答by Michael Pralow
i would rather use Hibernate Statistics published via JMXif you use spring you can make it real easy with Hibernate Statistics MBean with Spring
如果您使用 spring,我宁愿使用通过 JMX 发布的 Hibernate Statistics,您可以使用带有 Spring 的 Hibernate Statistics MBean使其变得非常容易
回答by Hons
In our application we published it via JMX and to make it complete we hat to kind of manually add the criteria query data using aspects
回答by Koekiebox
You can also add a logger for it. See; http://www.thoughts-on-java.org/how-to-activate-hibernate-statistics-to-analyze-performance-issues/
您还可以为其添加记录器。看; http://www.thoughts-on-java.org/how-to-activate-hibernate-statistics-to-analyze-performance-issues/
<!--Hibernate Statistics-->
<logger category="org.hibernate.stat" use-parent-handlers="true">
<level name="DEBUG"/>
</logger>
回答by Vlad Mihalcea
There are multiple ways you can access the Hibernate Statistics:
您可以通过多种方式访问 Hibernate 统计信息:
Programatically
以编程方式
If you want to get the Statistics
object in your application, you can do it as follows:
如果你想Statistics
在你的应用程序中获取对象,你可以这样做:
Session session = entityManager.unwrap(Session.class);
Statistics statistics = session.getSessionFactory().getStatistics();
Logging
日志记录
If you want to log the Statistics
report, you need to add the following log configuration entry:
如果要记录Statistics
报告,则需要添加以下日志配置条目:
<logger name="org.hibernate.engine.internal.StatisticalLoggingSessionEventListener" level="info"/>
JMX
JMX
You can also expose the Statistics
object via JMX, as explained in this article.
您还可以Statistics
通过 JMX公开对象,如本文所述。
For this, you need to set the following configuration property:
为此,您需要设置以下配置属性:
<property name="hibernate.jmx.enabled" value="true"/>
And locate the org.hibernate.core
MBean package in your JMX client application.
并org.hibernate.core
在 JMX 客户端应用程序中找到MBean 包。
If you want to get a better idea of what metrics Hibernate supports, then check out this article.
如果您想更好地了解 Hibernate 支持哪些指标,请查看这篇文章。