2 * Copyright 2000-2018 Vaadin Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
16 package com.vaadin.v7.data.util.sqlcontainer.query;
18 import java.io.Serializable;
19 import java.sql.Connection;
20 import java.sql.PreparedStatement;
21 import java.sql.ResultSet;
22 import java.sql.SQLException;
23 import java.sql.Statement;
25 import com.vaadin.v7.data.util.sqlcontainer.connection.JDBCConnectionPool;
28 * Common base class for database query classes that handle connections and
34 * @deprecated As of 8.0, no replacement available.
37 public abstract class AbstractTransactionalQuery implements Serializable {
39 private JDBCConnectionPool connectionPool;
40 private transient Connection activeConnection;
42 AbstractTransactionalQuery() {
45 AbstractTransactionalQuery(JDBCConnectionPool connectionPool) {
46 this.connectionPool = connectionPool;
50 * Reserves a connection with auto-commit off if no transaction is in
53 * @throws IllegalStateException
54 * if a transaction is already open
55 * @throws SQLException
56 * if a connection could not be obtained or configured
58 public void beginTransaction()
59 throws UnsupportedOperationException, SQLException {
60 if (isInTransaction()) {
61 throw new IllegalStateException("A transaction is already active!");
63 activeConnection = connectionPool.reserveConnection();
64 activeConnection.setAutoCommit(false);
68 * Commits (if not in auto-commit mode) and releases the active connection.
70 * @throws SQLException
71 * if not in a transaction managed by this query
73 public void commit() throws UnsupportedOperationException, SQLException {
74 if (!isInTransaction()) {
75 throw new SQLException("No active transaction");
77 if (!activeConnection.getAutoCommit()) {
78 activeConnection.commit();
80 connectionPool.releaseConnection(activeConnection);
81 activeConnection = null;
85 * Rolls back and releases the active connection.
87 * @throws SQLException
88 * if not in a transaction managed by this query
90 public void rollback() throws UnsupportedOperationException, SQLException {
91 if (!isInTransaction()) {
92 throw new SQLException("No active transaction");
94 activeConnection.rollback();
95 connectionPool.releaseConnection(activeConnection);
96 activeConnection = null;
100 * Check that a transaction is active.
102 * @throws SQLException
103 * if no active transaction
105 protected void ensureTransaction() throws SQLException {
106 if (!isInTransaction()) {
107 throw new SQLException("No active transaction!");
112 * Closes a statement and a resultset, then releases the connection if it is
113 * not part of an active transaction. A failure in closing one of the
114 * parameters does not prevent closing the rest.
116 * If the statement is a {@link PreparedStatement}, its parameters are
117 * cleared prior to closing the statement.
119 * Although JDBC specification does state that closing a statement closes
120 * its result set and closing a connection closes statements and result
121 * sets, this method does try to close the result set and statement
122 * explicitly whenever not null. This can guard against bugs in certain JDBC
123 * drivers and reduce leaks in case e.g. closing the result set succeeds but
124 * closing the statement or connection fails.
127 * the connection to release
129 * the statement to close, may be null to skip closing
131 * the result set to close, may be null to skip closing
132 * @throws SQLException
133 * if closing the result set or the statement fails
135 protected void releaseConnection(Connection conn, Statement statement,
136 ResultSet rs) throws SQLException {
143 if (null != statement) {
144 if (statement instanceof PreparedStatement) {
146 ((PreparedStatement) statement).clearParameters();
147 } catch (Exception e) {
148 // will be closed below anyway
155 releaseConnection(conn);
160 * Returns the currently active connection, reserves and returns a new
161 * connection if no active connection.
163 * @return previously active or newly reserved connection
164 * @throws SQLException
166 protected Connection getConnection() throws SQLException {
167 if (activeConnection != null) {
168 return activeConnection;
170 return connectionPool.reserveConnection();
173 protected boolean isInTransaction() {
174 return activeConnection != null;
178 * Releases the connection if it is not part of an active transaction.
181 * the connection to release
183 private void releaseConnection(Connection conn) {
184 if (conn != activeConnection && conn != null) {
185 connectionPool.releaseConnection(conn);