PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/bpel-store/src/main/java/org/apache/ode/store/hib/DbConfStoreConnectionFactory.java

https://github.com/apache/ode
Java | 203 lines | 143 code | 38 blank | 22 comment | 10 complexity | ad845f8a522d2cfb62bd60644ef1ba2a MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.apache.ode.store.hib;
  20. import java.sql.Connection;
  21. import java.sql.DatabaseMetaData;
  22. import java.sql.SQLException;
  23. import java.util.HashMap;
  24. import java.util.Map;
  25. import java.util.Properties;
  26. import java.util.concurrent.ConcurrentHashMap;
  27. import javax.sql.DataSource;
  28. import javax.transaction.TransactionManager;
  29. import org.slf4j.Logger;
  30. import org.slf4j.LoggerFactory;
  31. import org.apache.ode.bpel.iapi.BpelEngineException;
  32. import org.apache.ode.daohib.HibertenateJtaPlatform;
  33. import org.apache.ode.daohib.SessionManager;
  34. import org.apache.ode.store.ConfStoreConnectionFactory;
  35. import org.apache.ode.store.Messages;
  36. import org.apache.ode.utils.GUID;
  37. import org.apache.ode.utils.msg.MessageBundle;
  38. import org.hibernate.HibernateException;
  39. import org.hibernate.MappingException;
  40. import org.hibernate.SessionFactory;
  41. import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
  42. import org.hibernate.cfg.Configuration;
  43. import org.hibernate.cfg.Environment;
  44. import org.hibernate.service.ServiceRegistry;
  45. import org.hibernate.service.UnknownUnwrapTypeException;
  46. import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider;
  47. import org.hibernate.service.spi.Configurable;
  48. import org.hibernate.dialect.Dialect;
  49. public class DbConfStoreConnectionFactory implements ConfStoreConnectionFactory {
  50. private static final Logger __log = LoggerFactory.getLogger(DbConfStoreConnectionFactory.class);
  51. private static final Messages __msgs = MessageBundle.getMessages(Messages.class);
  52. private static final String _guid = new GUID().toString();
  53. private static final Map<String, DataSource> _dataSources = new ConcurrentHashMap<String, DataSource>();
  54. private TransactionManager _txMgr;
  55. private final DataSource _ds;
  56. final SessionFactory _sessionFactory;
  57. public DbConfStoreConnectionFactory(DataSource ds, Properties initialProps, boolean createDatamodel, String txFactoryClassName) {
  58. _ds = ds;
  59. // Don't want to pollute original properties
  60. Properties properties = new Properties();
  61. for (Object prop : initialProps.keySet()) {
  62. properties.put(prop, initialProps.get(prop));
  63. }
  64. __log.debug("using data source: " + ds);
  65. _dataSources.put(_guid, ds);
  66. if (createDatamodel) {
  67. properties.put(Environment.HBM2DDL_AUTO, "create-drop");
  68. }
  69. // Note that we don't allow the following properties to be overriden by the client.
  70. if (properties.containsKey(Environment.CONNECTION_PROVIDER))
  71. __log.warn("Ignoring user-specified Hibernate property: " + Environment.CONNECTION_PROVIDER);
  72. if (properties.containsKey(Environment.JTA_PLATFORM))
  73. __log.warn("Ignoring user-specified Hibernate property: " + Environment.JTA_PLATFORM);
  74. if (properties.containsKey(Environment.SESSION_FACTORY_NAME))
  75. __log.warn("Ignoring user-specified Hibernate property: " + Environment.SESSION_FACTORY_NAME);
  76. if (properties.containsKey(Environment.TRANSACTION_STRATEGY))
  77. __log.warn("Ignoring user-specified Hibernate property: " + Environment.TRANSACTION_STRATEGY);
  78. if (properties.containsKey(Environment.CURRENT_SESSION_CONTEXT_CLASS))
  79. __log.warn("Ignoring user-specified Hibernate property: " + Environment.CURRENT_SESSION_CONTEXT_CLASS);
  80. properties.put(SessionManager.PROP_GUID, _guid);
  81. properties.put(Environment.CONNECTION_PROVIDER, DataSourceConnectionProvider.class.getName());
  82. properties.put(Environment.JTA_PLATFORM, HibertenateJtaPlatform.class.getName());
  83. // Need to use CMTTransactionFactory instead of JTATransactionFactory in Hibernate 4
  84. // Refer: https://jira.spring.io/browse/SPR-9480?focusedCommentId=81419&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-81419
  85. properties.put(Environment.TRANSACTION_STRATEGY, "org.hibernate.transaction.CMTTransactionFactory");
  86. properties.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "jta");
  87. if(__log.isDebugEnabled()) __log.debug("Store connection properties: " + properties );
  88. initTxMgr(txFactoryClassName);
  89. SessionManager.registerTransactionManager(_guid, _txMgr);
  90. Configuration configuration = getDefaultConfiguration().setProperties(properties);
  91. ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
  92. _sessionFactory = configuration.buildSessionFactory(serviceRegistry);
  93. }
  94. public ConfStoreConnectionHib getConnection() {
  95. return new ConfStoreConnectionHib(_sessionFactory.getCurrentSession());
  96. }
  97. @SuppressWarnings("unchecked")
  98. private void initTxMgr(String txFactoryClassName) {
  99. __log.info("ProcessStore initializing transaction manager using " + txFactoryClassName);
  100. try {
  101. Class<?> txFactClass = getClass().getClassLoader().loadClass(txFactoryClassName);
  102. Object txFact = txFactClass.newInstance();
  103. _txMgr = (TransactionManager) txFactClass.getMethod("getTransactionManager", (Class[]) null).invoke(txFact);
  104. } catch (Exception e) {
  105. __log.error("Couldn't initialize a transaction manager with factory: " + txFactoryClassName, e);
  106. throw new RuntimeException("Couldn't initialize a transaction manager with factory: " + txFactoryClassName, e);
  107. }
  108. }
  109. public void beginTransaction() {
  110. try {
  111. _txMgr.begin();
  112. } catch (Exception e) {
  113. throw new RuntimeException(e);
  114. }
  115. }
  116. public void commitTransaction() {
  117. try {
  118. _txMgr.commit();
  119. } catch (Exception e) {
  120. throw new RuntimeException(e);
  121. }
  122. }
  123. public void rollbackTransaction() {
  124. try {
  125. _txMgr.rollback();
  126. } catch (Exception e) {
  127. throw new RuntimeException(e);
  128. }
  129. }
  130. static Configuration getDefaultConfiguration() throws MappingException {
  131. return new Configuration().addClass(ProcessConfDaoImpl.class).addClass(DeploymentUnitDaoImpl.class)
  132. .addClass(VersionTrackerDAOImpl.class);
  133. }
  134. public static class DataSourceConnectionProvider implements ConnectionProvider, Configurable {
  135. private String _guid;
  136. public DataSourceConnectionProvider() {
  137. }
  138. public void configure(Map props) throws HibernateException {
  139. _guid = (String) props.get(SessionManager.PROP_GUID);
  140. }
  141. public Connection getConnection() throws SQLException {
  142. return _dataSources.get(_guid).getConnection();
  143. }
  144. public void closeConnection(Connection arg0) throws SQLException {
  145. arg0.close();
  146. }
  147. public void close() throws HibernateException {
  148. }
  149. public boolean supportsAggressiveRelease() {
  150. return true;
  151. }
  152. public boolean isUnwrappableAs(Class unwrapType) {
  153. return ConnectionProvider.class.equals(unwrapType) ||
  154. DataSourceConnectionProvider.class.isAssignableFrom(unwrapType);
  155. }
  156. public <T> T unwrap(Class<T> unwrapType) {
  157. if (ConnectionProvider.class.equals(unwrapType) ||
  158. DataSourceConnectionProvider.class.isAssignableFrom(unwrapType)) {
  159. return (T) this;
  160. } else {
  161. throw new UnknownUnwrapTypeException( unwrapType );
  162. }
  163. }
  164. }
  165. }