X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=test%2Fenterprise%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Ftest%2Ftransactions%2FSimpleTransactionManager.java;h=c81eb9798d9b08862d979dce4301943271748871;hb=a6e781e3dc010072f0e6510d7a87157ce8bea04c;hp=ee8eb77dc08fb3d8a25ad5efb9bb154a86372750;hpb=b7f1440254cf470fec409c3aed6c109b91c1c916;p=utils diff --git a/test/enterprise/src/main/java/org/wamblee/test/transactions/SimpleTransactionManager.java b/test/enterprise/src/main/java/org/wamblee/test/transactions/SimpleTransactionManager.java index ee8eb77d..c81eb979 100644 --- a/test/enterprise/src/main/java/org/wamblee/test/transactions/SimpleTransactionManager.java +++ b/test/enterprise/src/main/java/org/wamblee/test/transactions/SimpleTransactionManager.java @@ -20,6 +20,8 @@ import java.util.List; import javax.transaction.UserTransaction; +import org.wamblee.general.ThreadSpecificProxyFactory; + /** * Simple transaction manager provides a simple mechanism to manage transaction * in test code through the {@link UserTransaction} object. @@ -30,7 +32,7 @@ import javax.transaction.UserTransaction; public class SimpleTransactionManager { private UserTransactionFactory factory; - private ThreadLocal current; + private ThreadSpecificProxyFactory transaction; private UserTransactionCallback callback; private List resources; @@ -42,12 +44,12 @@ public class SimpleTransactionManager { */ public SimpleTransactionManager(UserTransactionFactory aFactory) { factory = aFactory; - current = new ThreadLocal(); + transaction = new ThreadSpecificProxyFactory(UserTransaction.class); callback = new UserTransactionCallback() { @Override public void transactionFinished() { - current.set(null); + transaction.set(null); } }; resources = new ArrayList(); @@ -65,16 +67,27 @@ public class SimpleTransactionManager { } /** - * Gets a transaction associated with the current thread. + * Gets the user transaction. This is a contextual reference, meaning that + * it will delegate to the appropriate thread-specific user transaction. + * It is also safe to store in a JNDI tree and for caching by applications. * * @return User transaction. */ public UserTransaction getTransaction() { - UserTransaction transaction = current.get(); - if (transaction == null) { - transaction = factory.create(callback, resources); - current.set(transaction); + UserTransaction tx = transaction.get(); + if (tx == null) { + tx = factory.create(callback, resources); + transaction.set(tx); } - return transaction; + return transaction.getProxy(); + } + + /** + * Gets the thread-specific transaction object. + * @return Transaction object. + */ + UserTransaction getThreadSpecificTransaction() { + getTransaction(); // create tx if needed + return transaction.get(); } }