Mercurial > dive4elements > river
view flys-backend/src/main/java/de/intevation/flys/backend/SessionFactoryProvider.java @ 3341:51f037483071
Backend: Strategy to directly transform native SQL results into Maps.
flys-backend/trunk@4955 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 12 Jul 2012 14:51:23 +0000 |
parents | 69f06b83b3ec |
children | fb9dcc68b9c2 |
line wrap: on
line source
package de.intevation.flys.backend; import java.lang.management.ManagementFactory; import java.util.Properties; import javax.management.InstanceAlreadyExistsException; import javax.management.MBeanRegistrationException; import javax.management.MBeanServer; import javax.management.MalformedObjectNameException; import javax.management.NotCompliantMBeanException; import javax.management.ObjectName; import org.apache.log4j.Logger; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Environment; import org.hibernate.impl.SessionFactoryImpl; import org.hibernate.jmx.StatisticsService; public final class SessionFactoryProvider { private static Logger log = Logger.getLogger(SessionFactoryProvider.class); public static final boolean ENABLE_JMX = Boolean.getBoolean("flys.backend.enablejmx"); private static SessionFactory flysSessionFactory; private static SessionFactory sedDBSessionFactory; private SessionFactoryProvider() { } public static synchronized SessionFactory getSessionFactory() { if (flysSessionFactory == null) { flysSessionFactory = createSessionFactory(FLYSCredentials.getInstance()); } return flysSessionFactory; } public static SessionFactory createSessionFactory() { return createSessionFactory(FLYSCredentials.getDefault()); } public static synchronized SessionFactory getSedDBSessionFactory() { if (sedDBSessionFactory == null) { sedDBSessionFactory = createSessionFactory(SedDBCredentials.getInstance()); } return sedDBSessionFactory; } public static SessionFactory createSedDBSessionFactory() { return createSessionFactory(SedDBCredentials.getDefault()); } public static SessionFactory createSessionFactory( Credentials credentials ) { Configuration cfg = createConfiguration(credentials); 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(FLYSCredentials.getInstance()); } public static Configuration createConfiguration( Credentials credentials ) { Configuration cfg = new Configuration(); for (Class clazz: credentials.getClasses()) { cfg.addAnnotatedClass(clazz); } if (log.isDebugEnabled()) { log.debug("user: " + credentials.getUser()); log.debug("dialect: " + credentials.getDialect()); log.debug("driver: " + credentials.getDriver()); log.debug("url: " + credentials.getUrl()); } Properties props = new Properties(); // We rely on our own connection pool props.setProperty( "hibernate.connection.provider_class", "de.intevation.flys.utils.DBCPConnectionProvider"); props.setProperty(Environment.DIALECT, credentials.getDialect()); props.setProperty(Environment.USER, credentials.getUser()); props.setProperty(Environment.PASS, credentials.getPassword()); props.setProperty(Environment.DRIVER, credentials.getDriver()); props.setProperty(Environment.URL, credentials.getUrl()); cfg.mergeProperties(props); return cfg; } public static String getProperty(SessionFactoryImpl factory, String key) { Properties props = factory.getProperties(); return props.getProperty(key); } public static String getUser(SessionFactoryImpl factory) { return getProperty(factory, Environment.USER); } public static String getPass(SessionFactoryImpl factory) { return getProperty(factory, Environment.PASS); } public static String getURL(SessionFactoryImpl factory) { return getProperty(factory, Environment.URL); } public static String getDriver(SessionFactoryImpl factory) { return getProperty(factory, Environment.DRIVER); } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :