(no commit message)
[utils] / test / enterprise / src / main / java / org / wamblee / test / transactions / SimpleTransactionManager.java
index 3ba4e8147c760e19d76a410e25f4ec1d3f1f0224..a3d57102a6f2fe4d3f324e6d00598d00a65e0e4e 100644 (file)
@@ -21,41 +21,45 @@ import java.util.List;
 import javax.transaction.UserTransaction;
 
 import org.wamblee.general.ThreadSpecificProxyFactory;
+import org.wamblee.general.ThreadSpecificProxyFactory.CreationCallback;
 import org.wamblee.test.persistence.JpaBuilder;
 
 /**
  * 
  * <p>
  * Simple transaction manager provides a simple mechanism to manage transactions
- * in test code through the {@link UserTransaction} object.
- * To construct the transaction manager use: 
+ * in test code through the {@link UserTransaction} object. To construct the
+ * transaction manager use:
  * </p>
+ * 
  * <pre>
- *      SimpleTransactionManager manager = 
- *          new SimpleTransactionManager(new DefaultUserTransactionFactory());
+ * SimpleTransactionManager manager = new SimpleTransactionManager(
+ *     new DefaultUserTransactionFactory());
  * </pre>
  * 
  * <p>
  * Next, add resources to manage using {@link #addResource(TransactionResource)}:
+ * 
  * <pre>
- *      manager.addResource(jpaTester.getJpaBuilder());
+ * manager.addResource(jpaTester.getJpaBuilder());
  * </pre>
  * <p>
- * As you can see from the above, {@link JpaBuilder} is a resource that can be used
- * so this mechanism can be used with JPA testing. 
+ * As you can see from the above, {@link JpaBuilder} is a resource that can be
+ * used so this mechanism can be used with JPA testing.
  * </p>
  * 
  * <p>
- * The next step is to manage transactions using the standard <code>UserTransaction</code>
- * APIs:
+ * The next step is to manage transactions using the standard
+ * <code>UserTransaction</code> APIs:
  * </p>
+ * 
  * <pre>
  *      UserTransaction transaction = manager.getTransaction();
  *      transaction.begin();
  *     
  *      ... do work...
  *  
- *      transaction.commit(); 
+ *      transaction.commit();
  * </pre>
  * 
  * @author Erik Brakkee
@@ -64,9 +68,9 @@ import org.wamblee.test.persistence.JpaBuilder;
 public class SimpleTransactionManager {
 
     private UserTransactionFactory factory;
-    private ThreadLocal<UserTransaction> transaction; 
-    private UserTransactionCallback callback;
     private List<TransactionResource> resources;
+    private UserTransactionCallback transactionFInishedCallback;
+    private ThreadSpecificProxyFactory<UserTransaction> transaction;
 
     /**
      * Constructs the transaction manager.
@@ -76,15 +80,23 @@ public class SimpleTransactionManager {
      */
     public SimpleTransactionManager(UserTransactionFactory aFactory) {
         factory = aFactory;
-        transaction = new ThreadLocal<UserTransaction>();
-        callback = new UserTransactionCallback() {
 
+        resources = new ArrayList<TransactionResource>();
+
+        transactionFInishedCallback = new UserTransactionCallback() {
             @Override
             public void transactionFinished() {
-                transaction.set(null);
+                transaction.set(factory.create(this, resources)); 
             }
         };
-        resources = new ArrayList<TransactionResource>();
+        transaction = new ThreadSpecificProxyFactory<UserTransaction>(
+            UserTransaction.class, new CreationCallback<UserTransaction>() {
+                @Override
+                public UserTransaction create() {
+                    return factory.create(transactionFInishedCallback,
+                        resources);
+                }
+            });
     }
 
     /**
@@ -100,30 +112,22 @@ public class SimpleTransactionManager {
 
     /**
      * 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. 
+     * 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() {
-        return new AutoCreateUserTransaction(this);
-    }
-    
-    UserTransaction getOrCreateThreadSpecificTransaction() { 
-        UserTransaction tx = transaction.get();
-        if (tx == null) {
-            tx = factory.create(callback, resources);
-            transaction.set(tx);
-        }
-        return tx;
+        return transaction.getProxy();
     }
     
     /**
-     * Gets the thread-specific transaction object. 
-     * @return Transaction object. 
+     * Gets the thread-specific transaction object.
+     * 
+     * @return Transaction object.
      */
-    UserTransaction getThreadSpecificTransaction() { 
-       getTransaction(); // create tx if needed
-       return transaction.get();
+    UserTransaction getThreadSpecificTransaction() {
+        getTransaction(); // create tx if needed
+        return transaction.get();
     }
 }