summaryrefslogtreecommitdiff
path: root/src/main/java/com/amazon/carbonado/spi/TransactionScope.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/amazon/carbonado/spi/TransactionScope.java')
-rw-r--r--src/main/java/com/amazon/carbonado/spi/TransactionScope.java62
1 files changed, 52 insertions, 10 deletions
diff --git a/src/main/java/com/amazon/carbonado/spi/TransactionScope.java b/src/main/java/com/amazon/carbonado/spi/TransactionScope.java
index 28d8260..0ac2cee 100644
--- a/src/main/java/com/amazon/carbonado/spi/TransactionScope.java
+++ b/src/main/java/com/amazon/carbonado/spi/TransactionScope.java
@@ -41,6 +41,8 @@ import com.amazon.carbonado.Transaction;
* @see TransactionManager
*/
public class TransactionScope<Txn> {
+ private static final int OPEN = 0, DETACHED = 1, CLOSED = 2;
+
final TransactionManager<Txn> mTxnMgr;
final Lock mLock;
@@ -49,12 +51,12 @@ public class TransactionScope<Txn> {
// Tracks all registered cursors by storage type.
private Map<Class<?>, CursorList<TransactionImpl<Txn>>> mCursors;
- private boolean mClosed;
+ private int mState;
TransactionScope(TransactionManager<Txn> txnMgr, boolean closed) {
mTxnMgr = txnMgr;
mLock = new ReentrantLock(true);
- mClosed = closed;
+ mState = closed ? CLOSED : OPEN;
}
/**
@@ -118,7 +120,7 @@ public class TransactionScope<Txn> {
public <S extends Storable> void register(Class<S> type, Cursor<S> cursor) {
mLock.lock();
try {
- checkState();
+ checkClosed();
if (mCursors == null) {
mCursors = new IdentityHashMap<Class<?>, CursorList<TransactionImpl<Txn>>>();
}
@@ -177,7 +179,7 @@ public class TransactionScope<Txn> {
public Txn getTxn() throws Exception {
mLock.lock();
try {
- checkState();
+ checkClosed();
return mActive == null ? null : mActive.getTxn();
} finally {
mLock.unlock();
@@ -190,7 +192,7 @@ public class TransactionScope<Txn> {
public boolean isForUpdate() {
mLock.lock();
try {
- return (mClosed || mActive == null) ? false : mActive.isForUpdate();
+ return (mState == CLOSED || mActive == null) ? false : mActive.isForUpdate();
} finally {
mLock.unlock();
}
@@ -203,11 +205,51 @@ public class TransactionScope<Txn> {
public IsolationLevel getIsolationLevel() {
mLock.lock();
try {
- return (mClosed || mActive == null) ? null : mActive.getIsolationLevel();
+ return (mState == CLOSED || mActive == null) ? null : mActive.getIsolationLevel();
+ } finally {
+ mLock.unlock();
+ }
+ }
+
+ /**
+ * Attach this scope to the current thread, if it has been {@link
+ * TransactionManager#detachLocalScope detached}.
+ *
+ * @throws IllegalStateException if scope is not detached or if current
+ * thread already has a transaction scope
+ */
+ /*
+ public void attach() {
+ mLock.lock();
+ try {
+ if (mState != DETACHED) {
+ throw new IllegalStateException("Transaction scope is not detached");
+ }
+ mState = OPEN;
+ } finally {
+ mLock.unlock();
+ }
+ mTxnMgr.attach(this);
+ }
+ */
+
+ /**
+ * @return false if closed
+ */
+ /*
+ boolean detach() {
+ mLock.lock();
+ try {
+ if (mState == CLOSED) {
+ return false;
+ }
+ mState = DETACHED;
+ return true;
} finally {
mLock.unlock();
}
}
+ */
/**
* Exits all transactions and closes all cursors. Should be called only
@@ -216,7 +258,7 @@ public class TransactionScope<Txn> {
void close() throws RepositoryException {
mLock.lock();
try {
- if (!mClosed) {
+ if (mState != CLOSED) {
while (mActive != null) {
mActive.exit();
}
@@ -227,7 +269,7 @@ public class TransactionScope<Txn> {
}
}
} finally {
- mClosed = true;
+ mState = CLOSED;
mLock.unlock();
}
}
@@ -235,8 +277,8 @@ public class TransactionScope<Txn> {
/**
* Caller must hold mLock.
*/
- private void checkState() {
- if (mClosed) {
+ private void checkClosed() {
+ if (mState == CLOSED) {
throw new IllegalStateException("Repository is closed");
}
}