PageRenderTime 27ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/dao-hibernate/src/main/java/org/apache/ode/daohib/bpel/BpelDAOConnectionFactoryImpl.java

https://github.com/apache/ode
Java | 153 lines | 93 code | 25 blank | 35 comment | 14 complexity | 16f2e77ffcb9e3fea9c8c6c684279919 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.daohib.bpel;
  20. import java.sql.Connection;
  21. import java.sql.DatabaseMetaData;
  22. import java.util.Enumeration;
  23. import java.util.HashMap;
  24. import java.util.Properties;
  25. import javax.sql.DataSource;
  26. import javax.transaction.TransactionManager;
  27. import org.slf4j.Logger;
  28. import org.slf4j.LoggerFactory;
  29. import org.apache.ode.bpel.dao.BpelDAOConnection;
  30. import org.apache.ode.bpel.dao.BpelDAOConnectionFactoryJDBC;
  31. import org.apache.ode.daohib.DataSourceConnectionProvider;
  32. import org.apache.ode.daohib.HibertenateJtaPlatform;
  33. import org.apache.ode.daohib.SessionManager;
  34. import org.hibernate.HibernateException;
  35. import org.hibernate.cfg.Environment;
  36. import org.hibernate.dialect.Dialect;
  37. /**
  38. * Hibernate-based {@link org.apache.ode.bpel.dao.BpelDAOConnectionFactory}
  39. * implementation.
  40. */
  41. public class BpelDAOConnectionFactoryImpl implements BpelDAOConnectionFactoryJDBC {
  42. private static final Logger __log = LoggerFactory.getLogger(BpelDAOConnectionFactoryImpl.class);
  43. protected SessionManager _sessionManager;
  44. private DataSource _ds;
  45. private TransactionManager _tm;
  46. /**
  47. * Constructor.
  48. */
  49. public BpelDAOConnectionFactoryImpl() {
  50. }
  51. public BpelDAOConnection getConnection() {
  52. try {
  53. return new BpelDAOConnectionImpl(_sessionManager);
  54. } catch (HibernateException e) {
  55. __log.error("DbError", e);
  56. throw e;
  57. }
  58. }
  59. /**
  60. * @see org.apache.ode.bpel.dao.BpelDAOConnectionFactory#init(java.util.Properties)
  61. */
  62. public void init(Properties initialProps) {
  63. if (_ds == null) {
  64. String errmsg = "setDataSource() not called!";
  65. __log.error(errmsg);
  66. throw new IllegalStateException(errmsg);
  67. }
  68. if (_tm == null) {
  69. String errmsg = "setTransactionManager() not called!";
  70. __log.error(errmsg);
  71. throw new IllegalStateException(errmsg);
  72. }
  73. if (initialProps == null) initialProps = new Properties();
  74. // Don't want to pollute original properties
  75. Properties properties = new Properties();
  76. for (Object prop : initialProps.keySet()) {
  77. properties.put(prop, initialProps.get(prop));
  78. }
  79. // Note that we don't allow the following properties to be overriden by
  80. // the client.
  81. if (properties.containsKey(Environment.CONNECTION_PROVIDER))
  82. __log.warn("Ignoring user-specified Hibernate property: " + Environment.CONNECTION_PROVIDER);
  83. if (properties.containsKey(Environment.JTA_PLATFORM))
  84. __log.warn("Ignoring user-specified Hibernate property: " + Environment.JTA_PLATFORM);
  85. if (properties.containsKey(Environment.SESSION_FACTORY_NAME))
  86. __log.warn("Ignoring user-specified Hibernate property: " + Environment.SESSION_FACTORY_NAME);
  87. if (properties.containsKey(Environment.TRANSACTION_STRATEGY))
  88. __log.warn("Ignoring user-specified Hibernate property: " + Environment.TRANSACTION_STRATEGY);
  89. if (properties.containsKey(Environment.CURRENT_SESSION_CONTEXT_CLASS))
  90. __log.warn("Ignoring user-specified Hibernate property: " + Environment.CURRENT_SESSION_CONTEXT_CLASS);
  91. properties.put(Environment.CONNECTION_PROVIDER, DataSourceConnectionProvider.class.getName());
  92. properties.put(Environment.JTA_PLATFORM, HibertenateJtaPlatform.class.getName());
  93. // Need to use CMTTransactionFactory instead of JTATransactionFactory in Hibernate 4
  94. // Refer: https://jira.spring.io/browse/SPR-9480?focusedCommentId=81419&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-81419
  95. properties.put(Environment.TRANSACTION_STRATEGY, "org.hibernate.transaction.CMTTransactionFactory");
  96. properties.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "jta");
  97. // Isolation levels override; when you use a ConnectionProvider, this has no effect
  98. String level = System.getProperty("ode.connection.isolation", "2");
  99. properties.put(Environment.ISOLATION, level);
  100. if (__log.isDebugEnabled()) {
  101. Enumeration<?> names = properties.propertyNames();
  102. __log.debug("Properties passed to Hibernate:");
  103. while (names.hasMoreElements()) {
  104. String name = (String) names.nextElement();
  105. __log.debug(name + "=" + properties.getProperty(name));
  106. }
  107. }
  108. _sessionManager = createSessionManager(properties, _ds, _tm);
  109. }
  110. protected SessionManager createSessionManager(Properties properties, DataSource ds, TransactionManager tm) {
  111. return new SessionManager(properties, ds, tm);
  112. }
  113. public void shutdown() {
  114. _sessionManager.shutdown();
  115. }
  116. public void setDataSource(DataSource ds) {
  117. _ds = ds;
  118. }
  119. public DataSource getDataSource() {
  120. return _ds;
  121. }
  122. public void setTransactionManager(Object tm) {
  123. _tm = (TransactionManager) tm;
  124. }
  125. public void setUnmanagedDataSource(DataSource ds) {
  126. // Hibernate doesn't use this.
  127. }
  128. }