Prerequisites

Enable JMX support inside Hibernate - Using the Spring Framework

Hibernate does not publish MBeans by default. The following snippet can be used when running inside a Spring environment.

<bean id="jmxExporter" class="org.springframework.jmx.export.MBeanExporter">
    <property name="beans">
        <map>
            <entry key="Hibernate:application=Statistics" value-ref="hibernateStatisticsBean"/>
        </map>
    </property>
</bean>

<bean id="hibernateStatisticsBean" class="org.hibernate.jmx.StatisticsService">
    <property name="statisticsEnabled" value="true"/>
    <property name="sessionFactory">
    	<!-- Uncomment the following when using Hibernate directly -->
        <!-- ref local="sessionFactory"/ -->

        <!-- Uncomment the following when using Hibernate via JPA
             (Assuming the managed EMF is named 'entityManagerFactory'.
              Namespace is: xmlns:util="http://www.springframework.org/schema/util")
        -->
        <!-- util:property-path path="entityManagerFactory.sessionFactory" /-->
    </property>
</bean>

Enable JMX support inside Hibernate - Using the API

Alternatively the following Java code can be used to publish the MBeans.

SessionFactory sf = ...;
StatisticsService statsMBean = new StatisticsService();
statsMBean.setSessionFactory(sf);
statsMBean.setStatisticsEnabled(true);

MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
mBeanServer.registerMBean(statsMBean, new ObjectName("Hibernate:application=Statistics"));
Hibernate in JPA environment:
EntityManagerFactory emf = ...;
SessionFactory sf = ((HibernateEntityManagerFactory)emf).getSessionFactory();

Enable the generation of statistics inside Hibernate

Make sure you set the hibernate property "hibernate.generate_statistics" to true inside "persistence.xml" or any other supported configuration file.

<prop key="hibernate.generate_statistics">true</prop>

Using the plugin with JConsole

Quickstart with JConsole
java -jar hibernate-jconsole-1.0.7.jar

Note: From version 1.0.4 the plugin automatically searches the JConsole executable and passes all given arguments. When connecting to a locally monitored process, the hibernate jars are automatically detected and added to JConsole's classpath.


Quickstart with JConsole - Prior to 1.0.4
cd C:\Sandbox\Libraries\hibernate
jconsole -pluginpath C:\Sandbox\hibernate-jconsole-1.0.7.jar
				

Important Note: Make sure you start JConsole from a working path which contains "hibernate3.jar" as this is required by the plugin to work correctly.


Customizations
jconsole -pluginpath C:\Sandbox\hibernate-jconsole-1.0.7.jar \
		-J-Dhibernate.classpath=C:\Sandbox\Libraries\hibernate\hibernate3.jar \
		-J-Dhibernate.mbean=Hibernate:application=Statistics
				
  • "hibernate.classpath": Points to a jar or directory of jars that contains the hibernate jars.

    (If the path is set to a directory, the filename pattern "hibernate*.jar" is used to add all matching jars to the system classpath)
  • "hibernate.mbean": Specifies the object name of the Hibernate statistics Mbean for the case that it wasn't published under the name suggested above.

Using the plugin with VisualVm

Quickstart with VisualVm

Make sure you installed the plugin "VisualVm-JConsole" and configured the "JConsole-Plugin" path inside the "Options" menu to point to "hibernate-jconsole-1.0.7.jar".

Restart VisualVm and start monitoring Hibernate applications.

When monitoring remote application, restart VisualVM either in the correct working path or by adding the customization parameters inside the commandline as shown below. (see Customizations for more details)

visualvm -J-Dhibernate.classpath=C:\Sandbox\Libraries\hibernate\hibernate3.jar \
		-J-Dhibernate.mbean=Hibernate:application=Statistics