From f4b5012d20abe94c62e8d3b3c3d8ed626dff925b Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Tue, 23 Oct 2007 18:09:08 +0000 Subject: Deprecated use of ExceptionTransformer in TransactionManager. --- .../repo/jdbc/JDBCTransactionManager.java | 35 ++++++++++++++-------- .../repo/sleepycat/BDBTransactionManager.java | 23 ++++++++++---- .../amazon/carbonado/spi/TransactionManager.java | 28 ++++++++++++++--- 3 files changed, 64 insertions(+), 22 deletions(-) (limited to 'src/main/java/com/amazon') 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 7fa3fcc..862152d 100644 --- a/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCTransactionManager.java +++ b/src/main/java/com/amazon/carbonado/repo/jdbc/JDBCTransactionManager.java @@ -24,6 +24,7 @@ import java.sql.SQLException; import com.amazon.carbonado.FetchException; import com.amazon.carbonado.IsolationLevel; +import com.amazon.carbonado.PersistException; import com.amazon.carbonado.Transaction; import com.amazon.carbonado.spi.TransactionManager; @@ -34,12 +35,14 @@ import com.amazon.carbonado.spi.TransactionManager; * @author Brian S O'Neill */ class JDBCTransactionManager extends TransactionManager { + private final JDBCExceptionTransformer mExTransformer; + // Weakly reference repository because thread locals are not cleaned up // very quickly. private final WeakReference mRepositoryRef; JDBCTransactionManager(JDBCRepository repository) { - super(repository.getExceptionTransformer()); + mExTransformer = repository.getExceptionTransformer(); mRepositoryRef = new WeakReference(repository); } @@ -75,20 +78,28 @@ class JDBCTransactionManager extends TransactionManager { return new JDBCTransaction(repo.getConnectionForTxn(level)); } - protected boolean commitTxn(JDBCTransaction txn) throws SQLException { - txn.commit(); - return true; + protected boolean commitTxn(JDBCTransaction txn) throws PersistException { + try { + txn.commit(); + return true; + } catch (Throwable e) { + throw mExTransformer.toPersistException(e); + } } - protected void abortTxn(JDBCTransaction txn) throws SQLException, FetchException { - Connection con; - if ((con = txn.abort()) != null) { - JDBCRepository repo = mRepositoryRef.get(); - if (repo == null) { - con.close(); - } else { - repo.yieldConnection(con); + protected void abortTxn(JDBCTransaction txn) throws PersistException { + try { + Connection con; + if ((con = txn.abort()) != null) { + JDBCRepository repo = mRepositoryRef.get(); + if (repo == null) { + con.close(); + } else { + repo.yieldConnection(con); + } } + } catch (Throwable e) { + throw mExTransformer.toPersistException(e); } } } diff --git a/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBTransactionManager.java b/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBTransactionManager.java index 58a9e8d..f9b0c6c 100644 --- a/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBTransactionManager.java +++ b/src/main/java/com/amazon/carbonado/repo/sleepycat/BDBTransactionManager.java @@ -22,6 +22,7 @@ import java.lang.ref.WeakReference; import java.util.concurrent.TimeUnit; import com.amazon.carbonado.IsolationLevel; +import com.amazon.carbonado.PersistException; import com.amazon.carbonado.Transaction; import com.amazon.carbonado.spi.ExceptionTransformer; @@ -35,12 +36,14 @@ import com.amazon.carbonado.spi.TransactionManager; * @author Brian S O'Neill */ class BDBTransactionManager extends TransactionManager { + private final ExceptionTransformer mExTransformer; + // Weakly reference repository because thread locals are not cleaned up // very quickly and BDB environments hang on to a ton of memory. private final WeakReference> mRepositoryRef; BDBTransactionManager(ExceptionTransformer exTransformer, BDBRepository repository) { - super(exTransformer); + mExTransformer = exTransformer; mRepositoryRef = new WeakReference>(repository); } @@ -69,13 +72,21 @@ class BDBTransactionManager extends TransactionManager { } } - protected boolean commitTxn(Txn txn) throws Exception { - repository().txn_commit(txn); - return false; + protected boolean commitTxn(Txn txn) throws PersistException { + try { + repository().txn_commit(txn); + return false; + } catch (Throwable e) { + throw mExTransformer.toPersistException(e); + } } - protected void abortTxn(Txn txn) throws Exception { - repository().txn_abort(txn); + protected void abortTxn(Txn txn) throws PersistException { + try { + repository().txn_abort(txn); + } catch (Throwable e) { + throw mExTransformer.toPersistException(e); + } } private BDBRepository repository() { diff --git a/src/main/java/com/amazon/carbonado/spi/TransactionManager.java b/src/main/java/com/amazon/carbonado/spi/TransactionManager.java index 355af7c..676a15d 100644 --- a/src/main/java/com/amazon/carbonado/spi/TransactionManager.java +++ b/src/main/java/com/amazon/carbonado/spi/TransactionManager.java @@ -50,6 +50,9 @@ public abstract class TransactionManager { private boolean mClosed; + /** + * @deprecated + */ public TransactionManager(ExceptionTransformer exTransformer) { // The use of a fair lock is essential for shutdown hooks that attempt // to acquire the locks of all TransactionManagers. Otherwise, the @@ -58,8 +61,14 @@ public abstract class TransactionManager { mExTransformer = exTransformer; } + public TransactionManager() { + this(null); + } + /** * Returns the exception transformer in use. + * + * @deprecated */ public ExceptionTransformer getExceptionTransformer() { return mExTransformer; @@ -337,12 +346,12 @@ public abstract class TransactionManager { * * @return true if transaction object is still valid */ - protected abstract boolean commitTxn(Txn txn) throws Exception; + protected abstract boolean commitTxn(Txn txn) throws PersistException; /** * Aborts and closes the given internal transaction. */ - protected abstract void abortTxn(Txn txn) throws Exception; + protected abstract void abortTxn(Txn txn) throws PersistException; private static class TransactionImpl implements Transaction { private final TransactionManager mTxnMgr; @@ -393,9 +402,15 @@ public abstract class TransactionManager { if (!txnMgr.commitTxn(mTxn)) { mTxn = null; } + } catch (PersistException e) { + mTxn = null; + throw e; } catch (Throwable e) { mTxn = null; - throw txnMgr.mExTransformer.toPersistException(e); + if (txnMgr.mExTransformer != null) { + throw txnMgr.mExTransformer.toPersistException(e); + } + throw new PersistException(e); } } else { // Indicate fake nested transaction committed. @@ -424,8 +439,13 @@ public abstract class TransactionManager { if (mParent == null || mParent.mTxn != mTxn) { try { txnMgr.abortTxn(mTxn); + } catch (PersistException e) { + throw e; } catch (Throwable e) { - throw txnMgr.mExTransformer.toPersistException(e); + if (txnMgr.mExTransformer != null) { + throw txnMgr.mExTransformer.toPersistException(e); + } + throw new PersistException(e); } } } finally { -- cgit v1.2.3