From 67ae8e7430ed8222ba7582fcf3dfd8ce62b62c5d Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Sun, 4 Nov 2007 00:36:29 +0000 Subject: Reduce creation of unnecessary nested transactions. Added feature to JDBCRepository to suppress Storable reloading after insert or update. --- .../amazon/carbonado/spi/TransactionManager.java | 38 ++++++++++++++++++---- 1 file changed, 32 insertions(+), 6 deletions(-) (limited to 'src/main/java/com/amazon/carbonado/spi') diff --git a/src/main/java/com/amazon/carbonado/spi/TransactionManager.java b/src/main/java/com/amazon/carbonado/spi/TransactionManager.java index f16f548..fe08a01 100644 --- a/src/main/java/com/amazon/carbonado/spi/TransactionManager.java +++ b/src/main/java/com/amazon/carbonado/spi/TransactionManager.java @@ -249,7 +249,7 @@ public abstract class TransactionManager { /** * Returns null if no transaction is in progress. * - * @throws Exception thrown by createTxn + * @throws Exception thrown by createTxn or reuseTxn */ public Txn getTxn() throws Exception { mLock.lock(); @@ -341,6 +341,17 @@ public abstract class TransactionManager { return createTxn(parent, level); } + /** + * Called when a transaction is about to be reused. The default + * implementation of this method does nothing. Override if any preparation + * is required to ready a transaction for reuse. + * + * @param txn transaction to reuse, never null + * @since 1.1.3 + */ + protected void reuseTxn(Txn txn) throws Exception { + } + /** * Commits and closes the given internal transaction. * @@ -502,13 +513,28 @@ public abstract class TransactionManager { // Caller must hold mLock. Txn getTxn() throws Exception { - if (mTxn == null) { - TransactionManager txnMgr = mTxnMgr; - Txn parent = (mParent == null || mTop) ? null : mParent.getTxn(); + TransactionManager txnMgr = mTxnMgr; + if (mTxn != null) { + txnMgr.reuseTxn(mTxn); + } else { + Txn parentTxn; + if (mParent == null || mTop) { + parentTxn = null; + } else if ((parentTxn = mParent.mTxn) == null) { + // No point in creating nested transaction if parent + // has never been used. Create parent transaction + // and use it in child transaction, just like a fake + // nested transaction. + if ((parentTxn = mParent.getTxn()) != null) { + return mTxn = parentTxn; + } + // Isolation level of parent is none, so proceed to create + // a real transaction. + } if (mTimeoutUnit == null) { - mTxn = txnMgr.createTxn(parent, mLevel); + mTxn = txnMgr.createTxn(parentTxn, mLevel); } else { - mTxn = txnMgr.createTxn(parent, mLevel, mDesiredLockTimeout, mTimeoutUnit); + mTxn = txnMgr.createTxn(parentTxn, mLevel, mDesiredLockTimeout, mTimeoutUnit); } } return mTxn; -- cgit v1.2.3