From 2c3748324ded5ae7b0bd7c93b067af87846ece63 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Sat, 19 Mar 2011 19:42:53 +0000 Subject: Add TransactionMonitor. --- .../amazon/carbonado/txn/TransactionManager.java | 24 ++++++++++- .../amazon/carbonado/txn/TransactionMonitor.java | 47 ++++++++++++++++++++++ .../com/amazon/carbonado/txn/TransactionScope.java | 22 ++++++++-- 3 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/amazon/carbonado/txn/TransactionMonitor.java (limited to 'src/main/java/com') diff --git a/src/main/java/com/amazon/carbonado/txn/TransactionManager.java b/src/main/java/com/amazon/carbonado/txn/TransactionManager.java index dcd9671..19aec4d 100644 --- a/src/main/java/com/amazon/carbonado/txn/TransactionManager.java +++ b/src/main/java/com/amazon/carbonado/txn/TransactionManager.java @@ -39,15 +39,21 @@ public abstract class TransactionManager { private final ThreadLocal> mLocalScope; private final Map, ?> mAllScopes; + private final TransactionMonitor mMonitor; private int mState; public TransactionManager() { + this(null); + } + + public TransactionManager(TransactionMonitor monitor) { mLocalScope = new ThreadLocal>(); mAllScopes = new WeakIdentityMap(); + mMonitor = monitor; } - /** + /** * Returns the thread-local TransactionScope, creating it if needed. */ public TransactionScope localScope() { @@ -108,6 +114,22 @@ public abstract class TransactionManager { return false; } + // Called by TransactionScope. + void entered(Transaction txn, Transaction parent) { + TransactionMonitor monitor = mMonitor; + if (monitor != null) { + monitor.entered(txn, parent); + } + } + + // Called by TransactionScope. + void exited(Transaction txn, Transaction active) { + TransactionMonitor monitor = mMonitor; + if (monitor != null) { + monitor.exited(txn, active); + } + } + /** * Closes all transaction scopes. Should be called only when repository is * closed. diff --git a/src/main/java/com/amazon/carbonado/txn/TransactionMonitor.java b/src/main/java/com/amazon/carbonado/txn/TransactionMonitor.java new file mode 100644 index 0000000..2604dac --- /dev/null +++ b/src/main/java/com/amazon/carbonado/txn/TransactionMonitor.java @@ -0,0 +1,47 @@ +/* + * Copyright 2011 Amazon Technologies, Inc. or its affiliates. + * Amazon, Amazon.com and Carbonado are trademarks or registered trademarks + * of Amazon Technologies, Inc. or its affiliates. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.amazon.carbonado.txn; + +import com.amazon.carbonado.Transaction; + +/** + * Is notified as transactions enter and exit. Implementation must be thread-safe. + * + * @author Brian S O'Neill + * @see TransactionManager + */ +public interface TransactionMonitor { + /** + * Called by a thread which has just entered a transaction. + * + * @param entered transaction just entered + * @param parent optional parent of transaction; is null for top level transactions + */ + public abstract void entered(Transaction entered, Transaction parent); + + /** + * Called by a thread which has just exited a transaction. Only the first + * invocation of the exit method is passed to this monitor. + * + * @param exited transaction just exited + * @param active optional transaction which is now active; is null if the + * outermost transaction scope exited + */ + public abstract void exited(Transaction exited, Transaction active); +} diff --git a/src/main/java/com/amazon/carbonado/txn/TransactionScope.java b/src/main/java/com/amazon/carbonado/txn/TransactionScope.java index 7cf4903..2110ff6 100644 --- a/src/main/java/com/amazon/carbonado/txn/TransactionScope.java +++ b/src/main/java/com/amazon/carbonado/txn/TransactionScope.java @@ -90,7 +90,12 @@ public class TransactionScope { } } - return mActive = new TransactionImpl(this, parent, false, actualLevel); + TransactionImpl txn = new TransactionImpl(this, parent, false, actualLevel); + mActive = txn; + + mTxnMgr.entered(txn, parent); + + return txn; } finally { mLock.unlock(); } @@ -113,12 +118,23 @@ public class TransactionScope { ("Desired isolation level not supported: " + level); } - return mActive = new TransactionImpl(this, mActive, true, actualLevel); + TransactionImpl txn = new TransactionImpl(this, mActive, true, actualLevel); + mActive = txn; + + mTxnMgr.entered(txn, null); + + return txn; } finally { mLock.unlock(); } } + // Called by TransactionImpl with lock held. + void exited(TransactionImpl txn, TransactionImpl active) { + mActive = active; + mTxnMgr.exited(txn, active); + } + /** * Registers the given cursor against the active transaction, allowing it * to be closed on transaction exit or transaction manager close. If there @@ -494,8 +510,8 @@ public class TransactionScope { } } } finally { - scope.mActive = mParent; mState = EXITED; + scope.exited(this, mParent); if (exception != null) { throw ExceptionTransformer.getInstance().toPersistException(exception); } -- cgit v1.2.3