Version 6

    Obtaining Statistics on the Hibernate Session usage and JPA Hibernate EntityManager can be very useful in Performance analysis.Hibernate does not gather statistics by default.Statistics capture must be enabled for a Hibernate SessionFactory.  When the  statistics are enabled all of the metrics discussed in org.hibernate.stat.Statistics can be collected.



    The method discussed in this article could be used for any application using JPA and Hibernate including a Seam Application. Using a Managed Bean, we can reduce the impact to the code in an existing application, since only the addition of extra file in the META-INF folder is required and in most cases only two additional lines are needed in an existing persistence.xml file.

    For an EJB3 project, We will add a file named hibernate-service.xml to the META-INF folder, this file defines the Managed Bean needed to collect the statistics, important to note here are that the name of the mbean(ie, Hibernate:application=BookingApp) should be unique per the Application Server instance and that the SessionFactory attribute references a value defined in the persistence.xml:


    <?xml version="1.0" encoding="UTF-8"?>
          <mbean code="org.hibernate.jmx.StatisticsService"
                    <attribute name="SessionFactoryJNDIName">bookingSF</attribute>

    The persistence.xml defines the Session Factory to be used by the mbean to report statistics usage and the property hibernate.generate_statistics is set to true:


       <persistence-unit name="bookingDB">
             <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/>
             <property name="" value="update"/>
             <property name="hibernate.show_sql" value="true"/>
             <property name="hibernate.format_sql" value="true"/>
             <property name="hibernate.generate_statistics" value="true"/>
             <property name="hibernate.session_factory_name" value="bookingSF"/>
             <property name="" value="java:/bookingEntityManagerFactory"/>

    After this bring up the JBoss Operations Network (JON), jconsole, EAP 5 Admin Console or any other mbean viewer to verify that you are receiving the Hibernate Statistics on the entities.