summaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorJesse Morgan <jesse@jesterpm.net>2015-03-06 09:20:18 -0800
committerJesse Morgan <jesse@jesterpm.net>2015-03-06 09:20:18 -0800
commitbb5b9878e8a9e96161dea08b077258568bb66425 (patch)
treefd615ead540ccec049d415ad845fad54abc89d66 /src/main/java
parentf84d8732fc2ee009e6babcb31e7a9428b3e1b285 (diff)
parent714003911f00d63be0d2ab23a6028005150f9840 (diff)
Merge pull request #3 from Carbonado/fix-jdbc-leak
Fixing leaked JDBC Connections.
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/com/amazon/carbonado/repo/jdbc/JDBCTransaction.java15
-rw-r--r--src/main/java/com/amazon/carbonado/repo/jdbc/JDBCTransactionManager.java30
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);
}
}
}