27

So in my persistence.xml I turned on hibernate.generate_statistics.

<property name="hibernate.generate_statistics">true</property>

My question is how do I access them? Where do the statistics go?

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
mainstringargs
  • 13,563
  • 35
  • 109
  • 174

5 Answers5

25

In your dao service you can go:

Session session = this.sessionFactory.getCurrentSession();
SessionStatistics sessionStats = session.getStatistics();
Statistics stats = this.sessionFactory.getStatistics(); 
serg
  • 109,619
  • 77
  • 317
  • 330
13

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

Michael Pralow
  • 6,560
  • 2
  • 30
  • 46
11

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.

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
  • From Operations point of view: JMX is the best option since APM tools can collect the data from JMX; From Development point of view, in local box the JConsole tool from JDK can view the data easily – Happy Nov 28 '22 at 21:59
4

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>
Koekiebox
  • 5,793
  • 14
  • 53
  • 88
2

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

Hons
  • 3,804
  • 3
  • 32
  • 50