summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBrian S. O'Neill <bronee@gmail.com>2011-03-19 19:42:53 +0000
committerBrian S. O'Neill <bronee@gmail.com>2011-03-19 19:42:53 +0000
commit2c3748324ded5ae7b0bd7c93b067af87846ece63 (patch)
treead14223c753146c1cec097e21e802adf9d91b9d2 /src
parent766042105821ea01bcc745ea7d6347aaeea0a5b8 (diff)
Add TransactionMonitor.
Diffstat (limited to 'src')
-rw-r--r--src/main/java/com/amazon/carbonado/txn/TransactionManager.java24
-rw-r--r--src/main/java/com/amazon/carbonado/txn/TransactionMonitor.java47
-rw-r--r--src/main/java/com/amazon/carbonado/txn/TransactionScope.java22
3 files changed, 89 insertions, 4 deletions
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<Txn> {
private final ThreadLocal<TransactionScope<Txn>> mLocalScope;
private final Map<TransactionScope<Txn>, ?> mAllScopes;
+ private final TransactionMonitor mMonitor;
private int mState;
public TransactionManager() {
+ this(null);
+ }
+
+ public TransactionManager(TransactionMonitor monitor) {
mLocalScope = new ThreadLocal<TransactionScope<Txn>>();
mAllScopes = new WeakIdentityMap();
+ mMonitor = monitor;
}
- /**
+ /**
* Returns the thread-local TransactionScope, creating it if needed.
*/
public TransactionScope<Txn> localScope() {
@@ -108,6 +114,22 @@ public abstract class TransactionManager<Txn> {
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<Txn> {
}
}
- return mActive = new TransactionImpl<Txn>(this, parent, false, actualLevel);
+ TransactionImpl<Txn> txn = new TransactionImpl<Txn>(this, parent, false, actualLevel);
+ mActive = txn;
+
+ mTxnMgr.entered(txn, parent);
+
+ return txn;
} finally {
mLock.unlock();
}
@@ -113,12 +118,23 @@ public class TransactionScope<Txn> {
("Desired isolation level not supported: " + level);
}
- return mActive = new TransactionImpl<Txn>(this, mActive, true, actualLevel);
+ TransactionImpl<Txn> txn = new TransactionImpl<Txn>(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> txn, TransactionImpl<Txn> 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<Txn> {
}
}
} finally {
- scope.mActive = mParent;
mState = EXITED;
+ scope.exited(this, mParent);
if (exception != null) {
throw ExceptionTransformer.getInstance().toPersistException(exception);
}