(no commit message)
[utils] / test / enterprise / src / main / java / org / wamblee / test / transactions / SimpleTransactionManager.java
index ee8eb77dc08fb3d8a25ad5efb9bb154a86372750..c81eb9798d9b08862d979dce4301943271748871 100644 (file)
@@ -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<UserTransaction> current;
+    private ThreadSpecificProxyFactory<UserTransaction> transaction; 
     private UserTransactionCallback callback;
     private List<TransactionResource> resources;
 
@@ -42,12 +44,12 @@ public class SimpleTransactionManager {
      */
     public SimpleTransactionManager(UserTransactionFactory aFactory) {
         factory = aFactory;
-        current = new ThreadLocal<UserTransaction>();
+        transaction = new ThreadSpecificProxyFactory<UserTransaction>(UserTransaction.class);
         callback = new UserTransactionCallback() {
 
             @Override
             public void transactionFinished() {
-                current.set(null);
+                transaction.set(null);
             }
         };
         resources = new ArrayList<TransactionResource>();
@@ -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();
     }
 }