How to get access to the Hibernate Statistics
i would rather use Hibernate Statistics published via JMX if you use spring you can make it real easy with Hibernate Statistics MBean with Spring
You can also add a logger for it. See; 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>
In your dao service you can go:
Session session = this.sessionFactory.getCurrentSession();
SessionStatistics sessionStats = session.getStatistics();
Statistics stats = this.sessionFactory.getStatistics();
There are multiple ways you can access the Hibernate Statistics:
Programatically
If you want to get the Statistics
object in your application, you can do it as follows:
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:
<logger name="org.hibernate.engine.internal.StatisticalLoggingSessionEventListener" level="info"/>
JMX
You can also expose the Statistics
object via JMX by setting the hibernate.jmx.enabled
property.
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.