lundi 4 mars 2013

Optimize hibernate with JMX bean in tomcat application server

In order to optimize your web application application, you shall always profile the database layer to see what are the bottle necks. You can do this by using the hibernate JMX component org.hibernate.jmx.StatisticsService (providing that you use the hibernate session factory)

In your tomcat, add the line:
JAVA_OPTS=-Dcom.sun.management.jmxremote to unable JMX bean
In your spring context file add the lines:
<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
 <property name="beans">
  <map>
   <entry key="bean:name=hibernateStatBean" value-ref="hibernateStatBean"/>
  </map>
 </property>
</bean>

<bean id="hibernateStatBean" class="org.hibernate.jmx.StatisticsService">
 <property name="sessionFactory" ref="sessionFactory"/>
</bean>
  
In the hibernate spring configuration file add the following property to enable the statistics:

<bean id="hibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
 <property name="properties">
  <props>
   <prop key="hibernate.show_sql">false</prop>
   <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
   <prop key="hibernate.generate_statistics">true</prop>            
  </props>
 </property>
</bean>



If your tomcat application server starts without error, you can now observe hibernate parameters using JVisualVM utility bundled withi the JDK.

You must unable the JMX plugin in the menu Tools --> Plugin --> Available Plugins tab
install VisualVM-MBeans. You should have the following result.
Then goto the MBeans tab and choose the JMX bean HibernateStatBean.
The observed parameters are displayed in the Attributes tabs after you set statisticsEnabled to true and presh the refresh button in the attribute tab.

You may reset the MBean in the operation tab.


Some key parameters are:

StatisticsEnabled : you should check that it is set to true.
TransactionCount: Number of started transactions
QueryExecutionCount: Number of queries
QueryExecutionMaxTimeQueryString: Give your the request taking most time. The bottleneck.
EntityLoadCount: Give you the number of entity loaded



Aucun commentaire: