changeset 2337:52d712cd2384

Added code and a system property 'flys.backend.enablejmx=true' to enable JMX support for hibernate. flys-backend/trunk@2808 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 22 Sep 2011 11:20:15 +0000
parents 60c722dc0304
children e69ef36290ae
files flys-backend/ChangeLog flys-backend/src/main/java/de/intevation/flys/backend/SessionFactoryProvider.java
diffstat 2 files changed, 60 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/ChangeLog	Wed Sep 21 10:12:00 2011 +0000
+++ b/flys-backend/ChangeLog	Thu Sep 22 11:20:15 2011 +0000
@@ -1,3 +1,10 @@
+2011-09-22  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/backend/SessionFactoryProvider.java:
+	  Added the possibility to enable JMX (/MBean) support for hibernate. By
+	  default, this support is NOT enabled. To enable JMX support for
+	  hibernate, set the system property "flys.backend.enablejmx=true".
+
 2011-09-20  Bjoern Schilberg <bjoern@intevation.de>
 
 	* doc/schema/oracle_create_user.sql: Added right to create views.
--- a/flys-backend/src/main/java/de/intevation/flys/backend/SessionFactoryProvider.java	Wed Sep 21 10:12:00 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/backend/SessionFactoryProvider.java	Thu Sep 22 11:20:15 2011 +0000
@@ -2,10 +2,20 @@
 
 import de.intevation.artifacts.common.utils.Config;
 
+import java.lang.management.ManagementFactory;
 import java.util.Properties;
 
+import javax.management.InstanceAlreadyExistsException;
+import javax.management.MalformedObjectNameException;
+import javax.management.MBeanRegistrationException;
+import javax.management.MBeanServer;
+import javax.management.NotCompliantMBeanException;
+import javax.management.ObjectName;
+
 import org.hibernate.SessionFactory;
 
+import org.hibernate.jmx.StatisticsService;
+
 import org.hibernate.cfg.Configuration;
 import org.hibernate.cfg.Environment;
 
@@ -86,6 +96,9 @@
             "flys.backend.url",
             "jdbc:postgresql://localhost:5432/flys");
 
+    public static final boolean ENABLE_JMX =
+        Boolean.getBoolean("flys.backend.enablejmx");
+
     private static SessionFactory sessionFactory;
 
     private SessionFactoryProvider() {
@@ -129,9 +142,48 @@
         Configuration cfg = createConfiguration(
             user, password, dialect, driver, url);
 
-        return cfg.buildSessionFactory();
+        SessionFactory factory = cfg.buildSessionFactory();
+
+        if (ENABLE_JMX) {
+            registerAsMBean(factory);
+        }
+        else {
+            log.info("No JMX support for hibernate.");
+        }
+
+        return factory;
     }
 
+
+    public static void registerAsMBean(SessionFactory factory) {
+
+        StatisticsService statsMBean = new StatisticsService();
+        statsMBean.setSessionFactory(factory);
+        statsMBean.setStatisticsEnabled(true);
+
+        try {
+            MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
+            mbs.registerMBean(
+                statsMBean,
+                new ObjectName("Hibernate:application=Statistics"));
+
+            log.info("Enabled JMX support for hibernate.");
+        }
+        catch (MalformedObjectNameException mone) {
+            log.warn(mone, mone);
+        }
+        catch (InstanceAlreadyExistsException iaee) {
+            log.warn(iaee, iaee);
+        }
+        catch (MBeanRegistrationException mbre) {
+            log.warn(mbre, mbre);
+        }
+        catch (NotCompliantMBeanException ncmbe) {
+            log.warn(ncmbe, ncmbe);
+        }
+    }
+
+
     public static Configuration createConfiguration() {
         return createConfiguration(
             DEFAULT_USER,

http://dive4elements.wald.intevation.org