X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=test%2Fenterprise%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Ftest%2Ftransactions%2FSimpleTransactionManager.java;h=7cb51431c12942a1a3edc1dcec707ec885461a63;hb=83898516d61bfcd1f4e3a3ea79bb56f3877f4900;hp=c81eb9798d9b08862d979dce4301943271748871;hpb=a6e781e3dc010072f0e6510d7a87157ce8bea04c;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 c81eb979..7cb51431 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 @@ -21,10 +21,46 @@ 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; /** - * Simple transaction manager provides a simple mechanism to manage transaction - * in test code through the {@link UserTransaction} object. + * + *

+ * Simple transaction manager provides a simple mechanism to manage transactions + * in test code through the {@link UserTransaction} object. To construct the + * transaction manager use: + *

+ * + *
+ * SimpleTransactionManager manager = new SimpleTransactionManager(
+ *     new DefaultUserTransactionFactory());
+ * 
+ * + *

+ * Next, add resources to manage using {@link #addResource(TransactionResource)}: + * + *

+ * manager.addResource(jpaTester.getJpaBuilder());
+ * 
+ *

+ * 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. + *

+ * + *

+ * The next step is to manage transactions using the standard + * UserTransaction APIs: + *

+ * + *
+ *      UserTransaction transaction = manager.getTransaction();
+ *      transaction.begin();
+ *     
+ *      ... do work...
+ *  
+ *      transaction.commit();
+ * 
* * @author Erik Brakkee * @@ -32,9 +68,9 @@ import org.wamblee.general.ThreadSpecificProxyFactory; public class SimpleTransactionManager { private UserTransactionFactory factory; - private ThreadSpecificProxyFactory transaction; - private UserTransactionCallback callback; private List resources; + private UserTransactionCallback transactionFInishedCallback; + private ThreadSpecificProxyFactory transaction; /** * Constructs the transaction manager. @@ -44,15 +80,23 @@ public class SimpleTransactionManager { */ public SimpleTransactionManager(UserTransactionFactory aFactory) { factory = aFactory; - transaction = new ThreadSpecificProxyFactory(UserTransaction.class); - callback = new UserTransactionCallback() { + resources = new ArrayList(); + + transactionFInishedCallback = new UserTransactionCallback() { @Override public void transactionFinished() { - transaction.set(null); + transaction.set(factory.create(this, resources)); } }; - resources = new ArrayList(); + transaction = new ThreadSpecificProxyFactory( + UserTransaction.class, new CreationCallback() { + @Override + public UserTransaction create() { + return factory.create(transactionFInishedCallback, + resources); + } + }); } /** @@ -68,26 +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() { - UserTransaction tx = transaction.get(); - if (tx == null) { - tx = factory.create(callback, resources); - transaction.set(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(); } }