You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractTransactionalQuery.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.v7.data.util.sqlcontainer.query;
  17. import java.io.Serializable;
  18. import java.sql.Connection;
  19. import java.sql.PreparedStatement;
  20. import java.sql.ResultSet;
  21. import java.sql.SQLException;
  22. import java.sql.Statement;
  23. import com.vaadin.v7.data.util.sqlcontainer.connection.JDBCConnectionPool;
  24. /**
  25. * Common base class for database query classes that handle connections and
  26. * transactions.
  27. *
  28. * @author Vaadin Ltd
  29. * @since 6.8.9
  30. *
  31. * @deprecated As of 8.0, no replacement available.
  32. */
  33. @Deprecated
  34. public abstract class AbstractTransactionalQuery implements Serializable {
  35. private JDBCConnectionPool connectionPool;
  36. private transient Connection activeConnection;
  37. AbstractTransactionalQuery() {
  38. }
  39. AbstractTransactionalQuery(JDBCConnectionPool connectionPool) {
  40. this.connectionPool = connectionPool;
  41. }
  42. /**
  43. * Reserves a connection with auto-commit off if no transaction is in
  44. * progress.
  45. *
  46. * @throws IllegalStateException
  47. * if a transaction is already open
  48. * @throws SQLException
  49. * if a connection could not be obtained or configured
  50. */
  51. public void beginTransaction()
  52. throws UnsupportedOperationException, SQLException {
  53. if (isInTransaction()) {
  54. throw new IllegalStateException("A transaction is already active!");
  55. }
  56. activeConnection = connectionPool.reserveConnection();
  57. activeConnection.setAutoCommit(false);
  58. }
  59. /**
  60. * Commits (if not in auto-commit mode) and releases the active connection.
  61. *
  62. * @throws SQLException
  63. * if not in a transaction managed by this query
  64. */
  65. public void commit() throws UnsupportedOperationException, SQLException {
  66. if (!isInTransaction()) {
  67. throw new SQLException("No active transaction");
  68. }
  69. if (!activeConnection.getAutoCommit()) {
  70. activeConnection.commit();
  71. }
  72. connectionPool.releaseConnection(activeConnection);
  73. activeConnection = null;
  74. }
  75. /**
  76. * Rolls back and releases the active connection.
  77. *
  78. * @throws SQLException
  79. * if not in a transaction managed by this query
  80. */
  81. public void rollback() throws UnsupportedOperationException, SQLException {
  82. if (!isInTransaction()) {
  83. throw new SQLException("No active transaction");
  84. }
  85. activeConnection.rollback();
  86. connectionPool.releaseConnection(activeConnection);
  87. activeConnection = null;
  88. }
  89. /**
  90. * Check that a transaction is active.
  91. *
  92. * @throws SQLException
  93. * if no active transaction
  94. */
  95. protected void ensureTransaction() throws SQLException {
  96. if (!isInTransaction()) {
  97. throw new SQLException("No active transaction!");
  98. }
  99. }
  100. /**
  101. * Closes a statement and a resultset, then releases the connection if it is
  102. * not part of an active transaction. A failure in closing one of the
  103. * parameters does not prevent closing the rest.
  104. *
  105. * If the statement is a {@link PreparedStatement}, its parameters are
  106. * cleared prior to closing the statement.
  107. *
  108. * Although JDBC specification does state that closing a statement closes
  109. * its result set and closing a connection closes statements and result
  110. * sets, this method does try to close the result set and statement
  111. * explicitly whenever not null. This can guard against bugs in certain JDBC
  112. * drivers and reduce leaks in case e.g. closing the result set succeeds but
  113. * closing the statement or connection fails.
  114. *
  115. * @param conn
  116. * the connection to release
  117. * @param statement
  118. * the statement to close, may be null to skip closing
  119. * @param rs
  120. * the result set to close, may be null to skip closing
  121. * @throws SQLException
  122. * if closing the result set or the statement fails
  123. */
  124. protected void releaseConnection(Connection conn, Statement statement,
  125. ResultSet rs) throws SQLException {
  126. try {
  127. try {
  128. if (null != rs) {
  129. rs.close();
  130. }
  131. } finally {
  132. if (null != statement) {
  133. if (statement instanceof PreparedStatement) {
  134. try {
  135. ((PreparedStatement) statement).clearParameters();
  136. } catch (Exception e) {
  137. // will be closed below anyway
  138. }
  139. }
  140. statement.close();
  141. }
  142. }
  143. } finally {
  144. releaseConnection(conn);
  145. }
  146. }
  147. /**
  148. * Returns the currently active connection, reserves and returns a new
  149. * connection if no active connection.
  150. *
  151. * @return previously active or newly reserved connection
  152. * @throws SQLException
  153. */
  154. protected Connection getConnection() throws SQLException {
  155. if (activeConnection != null) {
  156. return activeConnection;
  157. }
  158. return connectionPool.reserveConnection();
  159. }
  160. protected boolean isInTransaction() {
  161. return activeConnection != null;
  162. }
  163. /**
  164. * Releases the connection if it is not part of an active transaction.
  165. *
  166. * @param conn
  167. * the connection to release
  168. */
  169. private void releaseConnection(Connection conn) {
  170. if (conn != activeConnection && conn != null) {
  171. connectionPool.releaseConnection(conn);
  172. }
  173. }
  174. }