Prerequisites
Using the plugin with JConsole
Using the plugin with VisualVm
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>
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"));
EntityManagerFactory emf = ...; SessionFactory sf = ((HibernateEntityManagerFactory)emf).getSessionFactory();
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>
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.
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.
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.
hibernate.mbean
":
Specifies the object name of the Hibernate statistics Mbean for the case that it wasn't
published under the name suggested above.
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