How to get access to the Hibernate Statistics

37,817

Solution 1

In your dao service you can go:

Session session = this.sessionFactory.getCurrentSession();
SessionStatistics sessionStats = session.getStatistics();
Statistics stats = this.sessionFactory.getStatistics(); 

Solution 2

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

Solution 3

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.

Solution 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>

Solution 5

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

Share:
37,817
mainstringargs
Author by

mainstringargs

Updated on July 09, 2022

Comments

  • mainstringargs
    mainstringargs almost 2 years

    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?