diff options
author | Jesse Morgan <morganjm@amazon.com> | 2015-03-05 15:09:26 -0800 |
---|---|---|
committer | Jesse Morgan <morganjm@amazon.com> | 2015-03-05 15:09:26 -0800 |
commit | 714003911f00d63be0d2ab23a6028005150f9840 (patch) | |
tree | fd615ead540ccec049d415ad845fad54abc89d66 /src/main/java | |
parent | f84d8732fc2ee009e6babcb31e7a9428b3e1b285 (diff) |
Fixing leaked JDBC Connections.
Fixing a leak if aborting a transaction throws an exception (e.g. request made
on closed connection).
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/com/amazon/carbonado/repo/jdbc/JDBCTransaction.java | 15 | ||||
-rw-r--r-- | src/main/java/com/amazon/carbonado/repo/jdbc/JDBCTransactionManager.java | 30 |
2 files changed, 31 insertions, 14 deletions
diff --git a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCTransaction.java b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCTransaction.java index ffa2e5c..00e6947 100644 --- a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCTransaction.java +++ b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCTransaction.java @@ -106,10 +106,17 @@ class JDBCTransaction { }
/**
- * @return connection to close, or null if not ready to because this was a
- * nested transaction
+ * @return true if the connection should be closed after the transaction is aborted.
*/
- Connection abort() throws SQLException {
+ boolean shouldCloseConnection() {
+ return !mIsNested;
+ }
+
+ /**
+ * Note: The caller should close the connection after aborting if
+ * shouldCloseConnection() returns true.
+ */
+ void abort() throws SQLException {
if (mRegisteredLobs != null) {
for (JDBCLob lob : mRegisteredLobs) {
lob.close();
@@ -134,13 +141,11 @@ class JDBCTransaction { }
}
- return null;
} else {
if (mReady) {
mConnection.rollback();
mReady = false;
}
- return mConnection;
}
}
diff --git a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCTransactionManager.java b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCTransactionManager.java index c649058..9fe2ff4 100644 --- a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCTransactionManager.java +++ b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCTransactionManager.java @@ -97,18 +97,30 @@ class JDBCTransactionManager extends TransactionManager<JDBCTransaction> { @Override
protected void abortTxn(JDBCTransaction txn) throws PersistException {
+ PersistException ex = null;
+
try {
- Connection con;
- if ((con = txn.abort()) != null) {
- JDBCRepository repo = mRepositoryRef.get();
- if (repo == null) {
- con.close();
- } else {
- repo.closeConnection(con);
+ txn.abort();
+ } catch (Throwable e) {
+ ex = mExTransformer.toPersistException(e);
+ throw ex;
+ } finally {
+ try {
+ if (txn.shouldCloseConnection()) {
+ Connection con = txn.getConnection();
+ JDBCRepository repo = mRepositoryRef.get();
+ if (repo == null) {
+ con.close();
+ } else {
+ repo.closeConnection(con);
+ }
+ }
+ } catch (Throwable e) {
+ // Don't lose the original exception.
+ if (ex == null) {
+ throw mExTransformer.toPersistException(e);
}
}
- } catch (Throwable e) {
- throw mExTransformer.toPersistException(e);
}
}
}
|