Mercurial > dive4elements > river
comparison flys-backend/src/main/java/de/intevation/flys/backend/SessionFactoryProvider.java @ 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 | 774dff3ee546 |
children | f834b411ca57 |
comparison
equal
deleted
inserted
replaced
2336:60c722dc0304 | 2337:52d712cd2384 |
---|---|
1 package de.intevation.flys.backend; | 1 package de.intevation.flys.backend; |
2 | 2 |
3 import de.intevation.artifacts.common.utils.Config; | 3 import de.intevation.artifacts.common.utils.Config; |
4 | 4 |
5 import java.lang.management.ManagementFactory; | |
5 import java.util.Properties; | 6 import java.util.Properties; |
6 | 7 |
8 import javax.management.InstanceAlreadyExistsException; | |
9 import javax.management.MalformedObjectNameException; | |
10 import javax.management.MBeanRegistrationException; | |
11 import javax.management.MBeanServer; | |
12 import javax.management.NotCompliantMBeanException; | |
13 import javax.management.ObjectName; | |
14 | |
7 import org.hibernate.SessionFactory; | 15 import org.hibernate.SessionFactory; |
16 | |
17 import org.hibernate.jmx.StatisticsService; | |
8 | 18 |
9 import org.hibernate.cfg.Configuration; | 19 import org.hibernate.cfg.Configuration; |
10 import org.hibernate.cfg.Environment; | 20 import org.hibernate.cfg.Environment; |
11 | 21 |
12 import de.intevation.flys.model.Annotation; | 22 import de.intevation.flys.model.Annotation; |
84 public static final String DEFAULT_URL = | 94 public static final String DEFAULT_URL = |
85 System.getProperty( | 95 System.getProperty( |
86 "flys.backend.url", | 96 "flys.backend.url", |
87 "jdbc:postgresql://localhost:5432/flys"); | 97 "jdbc:postgresql://localhost:5432/flys"); |
88 | 98 |
99 public static final boolean ENABLE_JMX = | |
100 Boolean.getBoolean("flys.backend.enablejmx"); | |
101 | |
89 private static SessionFactory sessionFactory; | 102 private static SessionFactory sessionFactory; |
90 | 103 |
91 private SessionFactoryProvider() { | 104 private SessionFactoryProvider() { |
92 } | 105 } |
93 | 106 |
127 String url | 140 String url |
128 ) { | 141 ) { |
129 Configuration cfg = createConfiguration( | 142 Configuration cfg = createConfiguration( |
130 user, password, dialect, driver, url); | 143 user, password, dialect, driver, url); |
131 | 144 |
132 return cfg.buildSessionFactory(); | 145 SessionFactory factory = cfg.buildSessionFactory(); |
133 } | 146 |
147 if (ENABLE_JMX) { | |
148 registerAsMBean(factory); | |
149 } | |
150 else { | |
151 log.info("No JMX support for hibernate."); | |
152 } | |
153 | |
154 return factory; | |
155 } | |
156 | |
157 | |
158 public static void registerAsMBean(SessionFactory factory) { | |
159 | |
160 StatisticsService statsMBean = new StatisticsService(); | |
161 statsMBean.setSessionFactory(factory); | |
162 statsMBean.setStatisticsEnabled(true); | |
163 | |
164 try { | |
165 MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); | |
166 mbs.registerMBean( | |
167 statsMBean, | |
168 new ObjectName("Hibernate:application=Statistics")); | |
169 | |
170 log.info("Enabled JMX support for hibernate."); | |
171 } | |
172 catch (MalformedObjectNameException mone) { | |
173 log.warn(mone, mone); | |
174 } | |
175 catch (InstanceAlreadyExistsException iaee) { | |
176 log.warn(iaee, iaee); | |
177 } | |
178 catch (MBeanRegistrationException mbre) { | |
179 log.warn(mbre, mbre); | |
180 } | |
181 catch (NotCompliantMBeanException ncmbe) { | |
182 log.warn(ncmbe, ncmbe); | |
183 } | |
184 } | |
185 | |
134 | 186 |
135 public static Configuration createConfiguration() { | 187 public static Configuration createConfiguration() { |
136 return createConfiguration( | 188 return createConfiguration( |
137 DEFAULT_USER, | 189 DEFAULT_USER, |
138 DEFAULT_PASSWORD, | 190 DEFAULT_PASSWORD, |