View RSS Feed

My Java Tips

SessionFactory Monitoring

Rate this Entry
by , 05-21-2011 at 10:37 PM (1811 Views)
There are few ways of accessing SessionFactory metrics. One of them is to use JMX to publish metrics. For that you need to enable the StatisticsService MBean. A single MBean can be enabled for your SessionFactory or one per factory.

Following snippet presents a configuration example:

Java Code:
// MBean service registration for a specific SessionFactory
Hashtable tb = new Hashtable();
tb.put("type", "statistics");
tb.put("sessionFactory", "myFinancialApp");
ObjectName on = new ObjectName("hibernate", tb); // MBean object name

StatisticsService stats = new StatisticsService(); // MBean implementation
stats.setSessionFactory(sessionFactory); // Bind the stats to a SessionFactory
server.registerMBean(stats, on); // Register the Mbean on the server
// MBean service registration for all SessionFactory's
Hashtable tb = new Hashtable();
tb.put("type", "statistics");
tb.put("sessionFactory", "all");
ObjectName on = new ObjectName("hibernate", tb); // MBean object name

StatisticsService stats = new StatisticsService(); // MBean implementation
server.registerMBean(stats, on); // Register the MBean on the server
Monitoring for a SessionFactory can be deactivated at configuration time, (setting hibernate.generate_statistics to false) and at runtime.

Java Code:
sf.getStatistics().setStatisticsEnabled(true) 
// or use the following
hibernateStatsBean.setStatisticsEnabled(true)
Use clear() method to reset the statistics. A summary can be sent to a logger (info level) using the logSummary() method.

Submit "SessionFactory Monitoring" to Facebook Submit "SessionFactory Monitoring" to Digg Submit "SessionFactory Monitoring" to del.icio.us Submit "SessionFactory Monitoring" to StumbleUpon Submit "SessionFactory Monitoring" to Google

Categories
Hibernate

Comments