From: erik Date: Sat, 17 Jul 2010 21:08:54 +0000 (+0000) Subject: (no commit message) X-Git-Tag: wamblee-utils-0.3~22 X-Git-Url: http://wamblee.org/gitweb/?a=commitdiff_plain;h=cd7c2439f09ef8d7f675f8bbfaa683fdac8f2733;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(); } } diff --git a/test/enterprise/src/test/java/org/wamblee/test/transactions/SimpleTransactionManagerTest.java b/test/enterprise/src/test/java/org/wamblee/test/transactions/SimpleTransactionManagerTest.java index 97c0359d..d0630ad8 100644 --- a/test/enterprise/src/test/java/org/wamblee/test/transactions/SimpleTransactionManagerTest.java +++ b/test/enterprise/src/test/java/org/wamblee/test/transactions/SimpleTransactionManagerTest.java @@ -63,14 +63,14 @@ public class SimpleTransactionManagerTest { @Test public void testTransactionsAreThreadSpecific() throws Exception { - UserTransaction transaction = manager.getTransaction(); + UserTransaction transaction = manager.getThreadSpecificTransaction(); assertNotNull(transaction); - assertSame(transaction, manager.getTransaction()); + assertSame(transaction, manager.getThreadSpecificTransaction()); final ValueHolder transaction2 = new ValueHolder(); Thread t = new Thread(new Runnable() { @Override public void run() { - transaction2.setValue(manager.getTransaction()); + transaction2.setValue(manager.getThreadSpecificTransaction()); } }); t.start(); @@ -82,11 +82,11 @@ public class SimpleTransactionManagerTest { @Test public void testTransactionLifeCycle() { - UserTransaction transaction = manager.getTransaction(); + UserTransaction transaction = manager.getThreadSpecificTransaction(); assertNotNull(transaction); assertEquals(1, callbacks.size()); callbacks.get(0).transactionFinished(); - UserTransaction transaction2 = manager.getTransaction(); + UserTransaction transaction2 = manager.getThreadSpecificTransaction(); assertNotNull(transaction2); assert (transaction != transaction2); }