Author: manik.surtani(a)jboss.com
Date: 2008-12-09 13:06:59 -0500 (Tue, 09 Dec 2008)
New Revision: 7271
Modified:
core/branches/flat/src/main/java/org/jboss/cache/transaction/TransactionTable.java
core/branches/flat/src/main/java/org/jboss/starobrno/atomic/AtomicHashMap.java
core/branches/flat/src/main/java/org/jboss/starobrno/atomic/AtomicHashMapProxy.java
core/branches/flat/src/main/java/org/jboss/starobrno/batch/AutoBatchSupport.java
core/branches/flat/src/main/java/org/jboss/starobrno/batch/BatchContainer.java
core/branches/flat/src/main/java/org/jboss/starobrno/context/TransactionContext.java
core/branches/flat/src/main/java/org/jboss/starobrno/context/TransactionContextImpl.java
core/branches/flat/src/main/java/org/jboss/starobrno/interceptors/BaseTransactionalContextInterceptor.java
core/branches/flat/src/main/java/org/jboss/starobrno/interceptors/InvocationContextInterceptor.java
core/branches/flat/src/main/java/org/jboss/starobrno/interceptors/TxInterceptor.java
core/branches/flat/src/main/java/org/jboss/starobrno/transaction/TransactionTable.java
core/branches/flat/src/main/java/org/jboss/starobrno/tree/TreeCacheImpl.java
core/branches/flat/src/main/java/org/jboss/starobrno/tree/TreeStructureSupport.java
Log:
better batch handling
Modified:
core/branches/flat/src/main/java/org/jboss/cache/transaction/TransactionTable.java
===================================================================
---
core/branches/flat/src/main/java/org/jboss/cache/transaction/TransactionTable.java 2008-12-09
18:03:13 UTC (rev 7270)
+++
core/branches/flat/src/main/java/org/jboss/cache/transaction/TransactionTable.java 2008-12-09
18:06:59 UTC (rev 7271)
@@ -226,7 +226,7 @@
StringBuilder sb = new StringBuilder();
sb.append("LocalTransactions:
").append(tx2gtxMap.size()).append("\n");
sb.append("GlobalTransactions:
").append(gtx2ContextMap.size()).append("\n");
- sb.append("tx2gtxMap:\n");
+ sb.append("txMapping:\n");
for (Map.Entry<Transaction, GlobalTransaction> entry : tx2gtxMap.entrySet())
{
sb.append(entry.getKey()).append(":
").append(entry.getValue()).append("\n");
Modified: core/branches/flat/src/main/java/org/jboss/starobrno/atomic/AtomicHashMap.java
===================================================================
---
core/branches/flat/src/main/java/org/jboss/starobrno/atomic/AtomicHashMap.java 2008-12-09
18:03:13 UTC (rev 7270)
+++
core/branches/flat/src/main/java/org/jboss/starobrno/atomic/AtomicHashMap.java 2008-12-09
18:06:59 UTC (rev 7271)
@@ -38,7 +38,7 @@
* @param <V>
*/
@NotThreadSafe
-public class AtomicHashMap<K, V> implements AtomicMap<K, V>, DeltaAware
+public class AtomicHashMap<K, V> implements AtomicMap<K, V>, DeltaAware,
Cloneable
{
FastCopyHashMap<K, V> delegate;
AtomicHashMapDelta delta = null;
@@ -162,10 +162,18 @@
public AtomicHashMap copyForWrite()
{
- AtomicHashMap clone = new AtomicHashMap();
- clone.delegate = (FastCopyHashMap) delegate.clone();
- clone.proxy = proxy;
- return clone;
+ try
+ {
+ AtomicHashMap clone = (AtomicHashMap) super.clone();
+ clone.delegate = (FastCopyHashMap) delegate.clone();
+ clone.proxy = proxy;
+ return clone;
+ }
+ catch (CloneNotSupportedException e)
+ {
+ // should never happen!!
+ throw new RuntimeException(e);
+ }
}
@Override
Modified:
core/branches/flat/src/main/java/org/jboss/starobrno/atomic/AtomicHashMapProxy.java
===================================================================
---
core/branches/flat/src/main/java/org/jboss/starobrno/atomic/AtomicHashMapProxy.java 2008-12-09
18:03:13 UTC (rev 7270)
+++
core/branches/flat/src/main/java/org/jboss/starobrno/atomic/AtomicHashMapProxy.java 2008-12-09
18:06:59 UTC (rev 7271)
@@ -43,7 +43,6 @@
{
this.cache = cache;
this.deltaMapKey = deltaMapKey;
- assertBatchingSupported(cache.getConfiguration());
this.batchContainer = ((CacheSPI) cache).getBatchContainer();
}
Modified:
core/branches/flat/src/main/java/org/jboss/starobrno/batch/AutoBatchSupport.java
===================================================================
---
core/branches/flat/src/main/java/org/jboss/starobrno/batch/AutoBatchSupport.java 2008-12-09
18:03:13 UTC (rev 7270)
+++
core/branches/flat/src/main/java/org/jboss/starobrno/batch/AutoBatchSupport.java 2008-12-09
18:06:59 UTC (rev 7271)
@@ -33,14 +33,6 @@
@NotThreadSafe
public abstract class AutoBatchSupport
{
- ThreadLocal<BatchDetails> batchDetails = new ThreadLocal<BatchDetails>()
- {
- @Override
- protected BatchDetails initialValue()
- {
- return new BatchDetails();
- }
- };
protected BatchContainer batchContainer;
protected void assertBatchingSupported(Configuration c)
@@ -51,26 +43,11 @@
protected void startAtomic()
{
- BatchDetails bd = batchDetails.get();
- if (!bd.startedBatch) bd.startedBatch = batchContainer.startBatch(true);
- bd.invocationCount++;
+ batchContainer.startBatch(true);
}
protected void endAtomic()
{
- BatchDetails bd = batchDetails.get();
- bd.invocationCount--;
- if (bd.startedBatch && bd.invocationCount == 0)
- {
- batchContainer.endBatch(true, true);
- bd.startedBatch = false;
- }
+ batchContainer.endBatch(true, true);
}
-
- private static class BatchDetails
- {
- boolean startedBatch;
- // this count is to deal with reentrancy from within the same class
- int invocationCount;
- }
}
Modified: core/branches/flat/src/main/java/org/jboss/starobrno/batch/BatchContainer.java
===================================================================
---
core/branches/flat/src/main/java/org/jboss/starobrno/batch/BatchContainer.java 2008-12-09
18:03:13 UTC (rev 7270)
+++
core/branches/flat/src/main/java/org/jboss/starobrno/batch/BatchContainer.java 2008-12-09
18:06:59 UTC (rev 7271)
@@ -37,13 +37,12 @@
public class BatchContainer
{
TransactionManager transactionManager;
- private ThreadLocal<Transaction> batchTransactionContainer = new
ThreadLocal<Transaction>();
- private ThreadLocal<Boolean> suspendTxAfterInvocation = new
ThreadLocal<Boolean>()
+ private ThreadLocal<BatchDetails> batchDetails = new
ThreadLocal<BatchDetails>()
{
@Override
- protected Boolean initialValue()
+ protected BatchDetails initialValue()
{
- return false;
+ return new BatchDetails();
}
};
@@ -66,17 +65,21 @@
public boolean startBatch(boolean autoBatch) throws CacheException
{
+ BatchDetails bd = batchDetails.get();
try
{
- if (transactionManager.getTransaction() == null &&
batchTransactionContainer.get() == null)
+ bd.invocationCount++;
+ if (transactionManager.getTransaction() == null && bd.tx == null)
{
transactionManager.begin();
- suspendTxAfterInvocation.set(!autoBatch);
+ bd.suspendTxAfterInvocation = !autoBatch;
+ // do not suspend if this is from an AutoBatch!
if (autoBatch)
- batchTransactionContainer.set(transactionManager.getTransaction());
+ bd.tx = transactionManager.getTransaction();
else
- batchTransactionContainer.set(transactionManager.suspend()); // do not
suspend if this is from an AutoBatch!
+ bd.tx = transactionManager.suspend();
+
return true;
}
return false;
@@ -85,6 +88,10 @@
{
throw new CacheException("Unable to start batch", e);
}
+ finally
+ {
+ batchDetails.set(bd);
+ }
}
public void endBatch(boolean success)
@@ -94,46 +101,61 @@
public void endBatch(boolean autoBatch, boolean success)
{
- Transaction tx = batchTransactionContainer.get();
- if (tx == null) return;
- Transaction existingTx = null;
- try
+ BatchDetails bd = batchDetails.get();
+ if (bd.tx == null) return;
+ if (autoBatch) bd.invocationCount--;
+ if (!autoBatch || bd.invocationCount == 0)
{
- existingTx = transactionManager.getTransaction();
+ Transaction existingTx = null;
+ try
+ {
+ existingTx = transactionManager.getTransaction();
- if ((existingTx == null && !autoBatch) || !tx.equals(existingTx))
- transactionManager.resume(tx);
+ if ((existingTx == null && !autoBatch) || !bd.tx.equals(existingTx))
+ transactionManager.resume(bd.tx);
- if (success)
- tx.commit();
- else
- tx.rollback();
- }
- catch (Exception e)
- {
- throw new CacheException("Unable to end batch", e);
- }
- finally
- {
- batchTransactionContainer.remove();
- try
- {
- if (!autoBatch && existingTx != null)
transactionManager.resume(existingTx);
+ if (success)
+ bd.tx.commit();
+ else
+ bd.tx.rollback();
}
catch (Exception e)
{
- throw new CacheException("Failed resuming existing transaction " +
existingTx, e);
+ throw new CacheException("Unable to end batch", e);
}
+ finally
+ {
+ batchDetails.remove();
+ try
+ {
+ if (!autoBatch && existingTx != null)
transactionManager.resume(existingTx);
+ }
+ catch (Exception e)
+ {
+ throw new CacheException("Failed resuming existing transaction "
+ existingTx, e);
+ }
+ }
}
+ else
+ {
+ batchDetails.set(bd);
+ }
}
public Transaction getBatchTransaction()
{
- return batchTransactionContainer.get();
+ return batchDetails.get().tx;
}
public boolean isSuspendTxAfterInvocation()
{
- return suspendTxAfterInvocation.get();
+ return batchDetails.get().suspendTxAfterInvocation;
}
+
+ private static class BatchDetails
+ {
+ int invocationCount;
+ boolean suspendTxAfterInvocation;
+ Transaction tx;
+ }
}
Modified:
core/branches/flat/src/main/java/org/jboss/starobrno/context/TransactionContext.java
===================================================================
---
core/branches/flat/src/main/java/org/jboss/starobrno/context/TransactionContext.java 2008-12-09
18:03:13 UTC (rev 7270)
+++
core/branches/flat/src/main/java/org/jboss/starobrno/context/TransactionContext.java 2008-12-09
18:06:59 UTC (rev 7271)
@@ -23,6 +23,7 @@
import org.jboss.starobrno.commands.VisitableCommand;
import org.jboss.starobrno.config.Option;
+import org.jboss.starobrno.transaction.GlobalTransaction;
import javax.transaction.Transaction;
import java.util.List;
@@ -89,6 +90,8 @@
*/
void setTransaction(Transaction tx);
+ void setGlobalTransaction(GlobalTransaction gtx);
+
/**
* Returns a local transaction associated with this context.
*
@@ -266,4 +269,6 @@
* Cleans up internal state, freeing up references.
*/
void reset();
+
+ GlobalTransaction getGobalTransaction();
}
Modified:
core/branches/flat/src/main/java/org/jboss/starobrno/context/TransactionContextImpl.java
===================================================================
---
core/branches/flat/src/main/java/org/jboss/starobrno/context/TransactionContextImpl.java 2008-12-09
18:03:13 UTC (rev 7270)
+++
core/branches/flat/src/main/java/org/jboss/starobrno/context/TransactionContextImpl.java 2008-12-09
18:06:59 UTC (rev 7271)
@@ -25,6 +25,7 @@
import org.jboss.starobrno.commands.VisitableCommand;
import org.jboss.starobrno.config.Option;
import org.jboss.starobrno.container.MVCCEntry;
+import org.jboss.starobrno.transaction.GlobalTransaction;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
@@ -77,6 +78,7 @@
private List<Object> removedNodes = null;
private final Map<Object, MVCCEntry> lookedUpEntries = new HashMap<Object,
MVCCEntry>(8);
+ private GlobalTransaction gtx;
public TransactionContextImpl(Transaction tx) throws SystemException,
RollbackException
{
@@ -149,6 +151,11 @@
lookedUpEntries.clear();
}
+ public GlobalTransaction getGobalTransaction()
+ {
+ return gtx;
+ }
+
public void putLookedUpEntries(Map<Object, MVCCEntry> entries)
{
lookedUpEntries.putAll(entries);
@@ -199,6 +206,11 @@
ltx = tx;
}
+ public void setGlobalTransaction(GlobalTransaction gtx)
+ {
+ this.gtx = gtx;
+ }
+
public Transaction getTransaction()
{
return ltx;
Modified:
core/branches/flat/src/main/java/org/jboss/starobrno/interceptors/BaseTransactionalContextInterceptor.java
===================================================================
---
core/branches/flat/src/main/java/org/jboss/starobrno/interceptors/BaseTransactionalContextInterceptor.java 2008-12-09
18:03:13 UTC (rev 7270)
+++
core/branches/flat/src/main/java/org/jboss/starobrno/interceptors/BaseTransactionalContextInterceptor.java 2008-12-09
18:06:59 UTC (rev 7271)
@@ -21,7 +21,6 @@
*/
package org.jboss.starobrno.interceptors;
-import org.jboss.starobrno.config.Option;
import org.jboss.starobrno.context.InvocationContext;
import org.jboss.starobrno.context.TransactionContext;
import org.jboss.starobrno.factories.annotations.Inject;
@@ -51,19 +50,6 @@
this.txTable = txTable;
}
- protected void copyInvocationScopeOptionsToTxScope(InvocationContext ctx)
- {
- // notify the transaction tCtx that this override is in place.
- TransactionContext tCtx = txTable.get(ctx.getGlobalTransaction());
- if (tCtx != null)
- {
- Option txScopeOption = new Option();
- txScopeOption.setCacheModeLocal(ctx.getOptionOverrides() != null &&
ctx.getOptionOverrides().isCacheModeLocal());
- txScopeOption.setSkipCacheStatusCheck(ctx.getOptionOverrides() != null
&& ctx.getOptionOverrides().isSkipCacheStatusCheck());
- tCtx.setOption(txScopeOption);
- }
- }
-
protected void setTransactionalContext(Transaction tx, GlobalTransaction gtx,
TransactionContext tCtx, InvocationContext ctx)
{
if (trace)
@@ -77,7 +63,7 @@
{
if (gtx != null)
{
- ctx.setTransactionContext(txTable.get(gtx));
+ ctx.setTransactionContext(txTable.getTransactionContext(gtx));
}
else if (tx == null)
{
Modified:
core/branches/flat/src/main/java/org/jboss/starobrno/interceptors/InvocationContextInterceptor.java
===================================================================
---
core/branches/flat/src/main/java/org/jboss/starobrno/interceptors/InvocationContextInterceptor.java 2008-12-09
18:03:13 UTC (rev 7270)
+++
core/branches/flat/src/main/java/org/jboss/starobrno/interceptors/InvocationContextInterceptor.java 2008-12-09
18:06:59 UTC (rev 7271)
@@ -22,21 +22,18 @@
package org.jboss.starobrno.interceptors;
+import org.jboss.starobrno.RPCManager;
import org.jboss.starobrno.commands.VisitableCommand;
import org.jboss.starobrno.commands.tx.CommitCommand;
import org.jboss.starobrno.commands.tx.PrepareCommand;
import org.jboss.starobrno.commands.tx.RollbackCommand;
-import org.jboss.starobrno.commands.write.ClearCommand;
-import org.jboss.starobrno.commands.write.PutKeyValueCommand;
-import org.jboss.starobrno.commands.write.PutMapCommand;
-import org.jboss.starobrno.commands.write.RemoveCommand;
-import org.jboss.starobrno.commands.write.ReplaceCommand;
+import org.jboss.starobrno.commands.write.*;
import org.jboss.starobrno.config.Option;
import org.jboss.starobrno.context.InvocationContext;
+import org.jboss.starobrno.context.TransactionContext;
import org.jboss.starobrno.factories.annotations.Inject;
import org.jboss.starobrno.transaction.GlobalTransaction;
import org.jboss.starobrno.transaction.TransactionTable;
-import org.jboss.starobrno.RPCManager;
import javax.transaction.SystemException;
import javax.transaction.Transaction;
@@ -237,4 +234,17 @@
{
return gtx != null && (gtx.getAddress() != null) &&
(!gtx.getAddress().equals(rpcManager.getLocalAddress()));
}
+
+ private void copyInvocationScopeOptionsToTxScope(InvocationContext ctx)
+ {
+ // notify the transaction tCtx that this override is in place.
+ TransactionContext tCtx = ctx.getTransactionContext();
+ if (tCtx != null)
+ {
+ Option txScopeOption = new Option();
+ txScopeOption.setCacheModeLocal(ctx.getOptionOverrides() != null &&
ctx.getOptionOverrides().isCacheModeLocal());
+ txScopeOption.setSkipCacheStatusCheck(ctx.getOptionOverrides() != null
&& ctx.getOptionOverrides().isSkipCacheStatusCheck());
+ tCtx.setOption(txScopeOption);
+ }
+ }
}
\ No newline at end of file
Modified:
core/branches/flat/src/main/java/org/jboss/starobrno/interceptors/TxInterceptor.java
===================================================================
---
core/branches/flat/src/main/java/org/jboss/starobrno/interceptors/TxInterceptor.java 2008-12-09
18:03:13 UTC (rev 7270)
+++
core/branches/flat/src/main/java/org/jboss/starobrno/interceptors/TxInterceptor.java 2008-12-09
18:06:59 UTC (rev 7271)
@@ -162,7 +162,7 @@
// remove from local lists.
transactions.remove(ltx);
// this tx has completed. Clean up in the tx table.
- txTable.remove(gtx, ltx);
+ txTable.cleanup(gtx);
}
if (log.isDebugEnabled()) log.debug("Finished remote rollback method for
" + gtx);
}
@@ -189,7 +189,7 @@
if (ltx == null)
{
log.warn("No local transaction for this remotely originating rollback.
Possibly rolling back before a prepare call was broadcast?");
- txTable.remove(gtx);
+ txTable.cleanup(gtx);
return null;
}
// disconnect if we have a current tx associated
@@ -221,7 +221,7 @@
transactions.remove(ltx);
// this tx has completed. Clean up in the tx table.
- txTable.remove(gtx, ltx);
+ txTable.cleanup(gtx);
}
if (log.isDebugEnabled()) log.debug("Finished remote commit/rollback method
for " + gtx);
}
@@ -278,7 +278,7 @@
// in the invocation context
Transaction ltx = txTable.getLocalTransaction(gtx);
Transaction currentTx = txManager.getTransaction();
-
+ TransactionContext transactionContext;
Object retval = null;
boolean success = false;
try
@@ -288,8 +288,10 @@
if (currentTx != null) txManager.suspend();
// create a new local transaction
ltx = createLocalTx();
+ if (log.isDebugEnabled()) log.debug("creating new tx
transactionContext");
+ transactionContext = contextFactory.createTransactionContext(ltx);
// associate this with a global tx
- txTable.put(ltx, gtx);
+ txTable.associateTransaction(ltx, gtx, transactionContext);
if (trace) log.trace("Created new tx for gtx " + gtx);
if (log.isDebugEnabled())
@@ -308,26 +310,28 @@
txManager.suspend();
txManager.resume(ltx);
}
+
+ // Asssociate the local TX with the global TX. Create new
+ // transactionContext for TX in txTable, the modifications
+ // below will need this transactionContext to add their modifications
+ // under the GlobalTx key
+ transactionContext = txTable.getTransactionContext(gtx);
+ if (transactionContext == null)
+ {
+ // create a new transaction transactionContext
+ if (log.isDebugEnabled()) log.debug("creating new tx
transactionContext");
+ transactionContext = contextFactory.createTransactionContext(ltx);
+ txTable.associateTransaction(ltx, gtx, transactionContext);
+ }
}
if (trace) log.trace("Resuming existing tx " + ltx + ", global
tx=" + gtx);
// at this point we have a non-null ltx
+// setTransactionalContext(ltx, gtx, transactionContext, ctx);
+ ctx.setTransactionContext(transactionContext);
+ ctx.setTransaction(ltx);
+ ctx.setGlobalTransaction(gtx);
- // Asssociate the local TX with the global TX. Create new
- // transactionContext for TX in txTable, the modifications
- // below will need this transactionContext to add their modifications
- // under the GlobalTx key
- TransactionContext transactionContext = txTable.get(gtx);
- if (transactionContext == null)
- {
- // create a new transaction transactionContext
- if (log.isDebugEnabled()) log.debug("creating new tx
transactionContext");
- transactionContext = contextFactory.createTransactionContext(ltx);
- txTable.put(gtx, transactionContext);
- }
-
- setTransactionalContext(ltx, gtx, transactionContext, ctx);
-
// register a sync handler for this tx.
registerHandler(ltx, new RemoteSynchronizationHandler(gtx, ltx,
transactionContext), ctx);
@@ -630,7 +634,7 @@
if (ctx.getGlobalTransaction() == null)
{
ctx.setGlobalTransaction(gtx);
- transactionContext = txTable.get(gtx);
+ transactionContext = txTable.getTransactionContext(gtx);
ctx.setTransactionContext(transactionContext);
}
else
@@ -803,8 +807,7 @@
finally
{
// clean up the tx table
- txTable.remove(gtx);
- txTable.remove(tx);
+ txTable.cleanup(gtx);
setTransactionalContext(null, null, null, ctx);
cleanupInternalState();
}
Modified:
core/branches/flat/src/main/java/org/jboss/starobrno/transaction/TransactionTable.java
===================================================================
---
core/branches/flat/src/main/java/org/jboss/starobrno/transaction/TransactionTable.java 2008-12-09
18:03:13 UTC (rev 7270)
+++
core/branches/flat/src/main/java/org/jboss/starobrno/transaction/TransactionTable.java 2008-12-09
18:06:59 UTC (rev 7271)
@@ -53,19 +53,15 @@
private static final boolean trace = log.isTraceEnabled();
/**
- * Mapping between local (javax.transaction.Transaction)
- * and GlobalTransactions.
- * New: a local TX can have a number of GTXs
+ * Mapping between local (javax.transaction.Transaction) and a TransactionContext
*/
- protected final Map<Transaction, GlobalTransaction> tx2gtxMap = new
ConcurrentHashMap<Transaction, GlobalTransaction>();
+ protected final Map<Transaction, TransactionContext> txMapping = new
ConcurrentHashMap<Transaction, TransactionContext>();
/**
- * Mappings between GlobalTransactions and modifications.
+ * Mappong between GlobalTransaction and a TransactionContext
*/
- protected final Map<GlobalTransaction, TransactionContext> gtx2ContextMap = new
ConcurrentHashMap<GlobalTransaction, TransactionContext>();
+ protected final Map<GlobalTransaction, TransactionContext> gtxMapping = new
ConcurrentHashMap<GlobalTransaction, TransactionContext>();
- protected final Map<GlobalTransaction, Transaction> gtx2TxMap = new
ConcurrentHashMap<GlobalTransaction, Transaction>();
-
private TransactionManager transactionManager = null;
private RPCManager rpcManager;
@@ -85,7 +81,7 @@
*/
public int getNumLocalTransactions()
{
- return tx2gtxMap.size();
+ return txMapping.size();
}
/**
@@ -93,7 +89,7 @@
*/
public int getNumGlobalTransactions()
{
- return gtx2ContextMap.size();
+ return txMapping.size();
}
/**
@@ -102,26 +98,12 @@
*/
public GlobalTransaction get(Transaction tx)
{
- if (tx == null)
- return null;
- return tx2gtxMap.get(tx);
+ if (tx == null) return null;
+ TransactionContext ctx = txMapping.get(tx);
+ return ctx == null ? null : ctx.getGobalTransaction();
}
/**
- * Returns the local transaction associated with a GlobalTransaction.
- *
- * @param gtx The GlobalTransaction
- * @return Transaction. The local transaction associated with a given
- * GlobalTransaction). This will be null if no local transaction is
- * associated with a given GTX
- */
- public Transaction getLocalTransaction(GlobalTransaction gtx)
- {
- if (gtx == null) return null;
- return gtx2TxMap.get(gtx);
- }
-
- /**
* If assers exists is true and the coresponding local transaction is null an
IllegalStateExcetpion is being thrown.
*/
public Transaction getLocalTransaction(GlobalTransaction gtx, boolean assertExists)
@@ -143,77 +125,36 @@
}
/**
- * Associates the global transaction with the local transaction.
+ * Associates 3 elements of a transaction - a local Transaction, a GlobalTransaction
and a TransactionContext -
+ * with each other.
+ *
+ * @param tx transaction to associate
+ * @param gtx global transaction to associate
+ * @param ctx transaction context to associate
*/
- public void put(Transaction tx, GlobalTransaction gtx)
+ public void associateTransaction(Transaction tx, GlobalTransaction gtx,
TransactionContext ctx)
{
- if (tx == null)
- {
- log.error("key (Transaction) is null");
- return;
- }
- tx2gtxMap.put(tx, gtx);
- gtx2TxMap.put(gtx, tx);
- }
+ if (ctx.getTransaction() == null) ctx.setTransaction(tx);
+ if (ctx.getGobalTransaction() == null) ctx.setGlobalTransaction(gtx);
- /**
- * Returns the local transaction entry for the global transaction.
- * Returns null if tx is null or it was not found.
- */
- public TransactionContext get(GlobalTransaction gtx)
- {
- return gtx != null ? gtx2ContextMap.get(gtx) : null;
+ txMapping.put(tx, ctx);
+ gtxMapping.put(gtx, ctx);
}
- /**
- * Associates the global transaction with a transaction context.
- */
- public void put(GlobalTransaction tx, TransactionContext transactionContext)
+ public Transaction getLocalTransaction(GlobalTransaction gtx)
{
- if (tx == null)
- {
- log.error("key (GlobalTransaction) is null");
- return;
- }
- gtx2ContextMap.put(tx, transactionContext);
+ TransactionContext ctx = gtxMapping.get(gtx);
+ return ctx == null ? null : ctx.getTransaction();
}
/**
- * Removes a global transation, returns the old transaction entry.
- */
- public TransactionContext remove(GlobalTransaction tx)
- {
- if (tx == null) return null;
- gtx2TxMap.remove(tx);
- return gtx2ContextMap.remove(tx);
- }
-
- /**
- * Removes a local transation, returns the global transaction entry.
- */
- public GlobalTransaction remove(Transaction tx)
- {
- if (tx == null)
- return null;
- return tx2gtxMap.remove(tx);
- }
-
- public void remove(GlobalTransaction gtx, Transaction tx)
- {
- gtx2TxMap.remove(gtx);
- gtx2ContextMap.remove(gtx);
- tx2gtxMap.remove(tx);
- }
-
- /**
* Returns summary debug information.
*/
@Override
public String toString()
{
StringBuilder sb = new StringBuilder();
- sb.append(tx2gtxMap.size()).append(" mappings, ");
- sb.append(gtx2ContextMap.size()).append(" transactions");
+ sb.append(txMapping.size()).append(" transactions");
return sb.toString();
}
@@ -225,18 +166,12 @@
if (!printDetails)
return toString();
StringBuilder sb = new StringBuilder();
- sb.append("LocalTransactions:
").append(tx2gtxMap.size()).append("\n");
- sb.append("GlobalTransactions:
").append(gtx2ContextMap.size()).append("\n");
- sb.append("tx2gtxMap:\n");
- for (Map.Entry<Transaction, GlobalTransaction> entry : tx2gtxMap.entrySet())
+ sb.append("Transactions:
").append(txMapping.size()).append("\n");
+ sb.append("mappings:\n");
+ for (Map.Entry<Transaction, TransactionContext> entry :
txMapping.entrySet())
{
sb.append(entry.getKey()).append(":
").append(entry.getValue()).append("\n");
}
- sb.append("gtx2EntryMap:\n");
- for (Map.Entry<GlobalTransaction, TransactionContext> transactionContextEntry
: gtx2ContextMap.entrySet())
- {
- sb.append(transactionContextEntry.getKey()).append(":
").append(transactionContextEntry.getValue()).append("\n");
- }
return sb.toString();
}
@@ -406,7 +341,6 @@
Address addr = rpcManager.getLocalAddress();
gtx = GlobalTransaction.create(addr);
if (trace) log.trace("Creating new GlobalTransaction " + gtx);
- put(tx, gtx);
TransactionContext transactionContext;
try
{
@@ -416,8 +350,7 @@
{
throw new CacheException("Unable to create a transaction entry!",
e);
}
-
- put(gtx, transactionContext);
+ associateTransaction(tx, gtx, transactionContext);
if (trace)
{
log.trace("created new GTX: " + gtx + ", local TX=" +
tx);
@@ -425,4 +358,15 @@
}
return gtx;
}
+
+ public TransactionContext getTransactionContext(GlobalTransaction gtx)
+ {
+ return gtxMapping.get(gtx);
+ }
+
+ public void cleanup(GlobalTransaction gtx)
+ {
+ TransactionContext ctx = gtxMapping.remove(gtx);
+ if (ctx != null) txMapping.remove(ctx.getTransaction());
+ }
}
Modified: core/branches/flat/src/main/java/org/jboss/starobrno/tree/TreeCacheImpl.java
===================================================================
---
core/branches/flat/src/main/java/org/jboss/starobrno/tree/TreeCacheImpl.java 2008-12-09
18:03:13 UTC (rev 7270)
+++
core/branches/flat/src/main/java/org/jboss/starobrno/tree/TreeCacheImpl.java 2008-12-09
18:06:59 UTC (rev 7271)
@@ -49,6 +49,7 @@
public TreeCacheImpl(Cache<K, V> cache)
{
super(cache);
+ assertBatchingSupported(cache.getConfiguration());
createRoot();
}
Modified:
core/branches/flat/src/main/java/org/jboss/starobrno/tree/TreeStructureSupport.java
===================================================================
---
core/branches/flat/src/main/java/org/jboss/starobrno/tree/TreeStructureSupport.java 2008-12-09
18:03:13 UTC (rev 7270)
+++
core/branches/flat/src/main/java/org/jboss/starobrno/tree/TreeStructureSupport.java 2008-12-09
18:06:59 UTC (rev 7271)
@@ -34,7 +34,6 @@
public TreeStructureSupport(Cache cache)
{
this.cache = (AtomicMapCache) cache;
- assertBatchingSupported(cache.getConfiguration());
batchContainer = ((CacheSPI) cache).getBatchContainer();
}