From: Erik Brakkee Date: Fri, 23 Jul 2010 14:44:30 +0000 (+0000) Subject: (no commit message) X-Git-Tag: wamblee-utils-0.7~205 X-Git-Url: http://wamblee.org/gitweb/?a=commitdiff_plain;h=9799245e4d4f5446b471633f0b1bc780ea96ccd1;p=utils --- diff --git a/test/enterprise/src/main/java/org/wamblee/test/transactions/AutoCreateUserTransaction.java b/test/enterprise/src/main/java/org/wamblee/test/transactions/AutoCreateUserTransaction.java deleted file mode 100644 index 7b5124be..00000000 --- a/test/enterprise/src/main/java/org/wamblee/test/transactions/AutoCreateUserTransaction.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright 2005-2010 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.wamblee.test.transactions; - -import javax.transaction.HeuristicMixedException; -import javax.transaction.HeuristicRollbackException; -import javax.transaction.NotSupportedException; -import javax.transaction.RollbackException; -import javax.transaction.SystemException; -import javax.transaction.UserTransaction; - -/** - * User transaction implemenetation that automatically creates thread specific transaction if needed. - * - * @author Erik Brakkee - * - */ -public class AutoCreateUserTransaction implements UserTransaction { - - private SimpleTransactionManager manager; - - public AutoCreateUserTransaction(SimpleTransactionManager aManager) { - manager = aManager; - } - - private UserTransaction getTransaction() { - return manager.getOrCreateThreadSpecificTransaction(); - } - - @Override - public void begin() throws NotSupportedException, SystemException { - getTransaction().begin(); - } - - @Override - public void commit() throws RollbackException, HeuristicMixedException, - HeuristicRollbackException, SecurityException, IllegalStateException, - SystemException { - getTransaction().commit(); - } - - @Override - public int getStatus() throws SystemException { - return getTransaction().getStatus(); - } - - @Override - public void rollback() throws IllegalStateException, SecurityException, - SystemException { - getTransaction().rollback(); - } - - @Override - public void setRollbackOnly() throws IllegalStateException, SystemException { - getTransaction().setRollbackOnly(); - } - - @Override - public void setTransactionTimeout(int aSeconds) throws SystemException { - getTransaction().setTransactionTimeout(aSeconds); - } - -} 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 3ba4e814..a3d57102 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,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; /** * *

* 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: *

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

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

- *      manager.addResource(jpaTester.getJpaBuilder());
+ * 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. + * 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: + * The next step is to manage transactions using the standard + * UserTransaction APIs: *

+ * *
  *      UserTransaction transaction = manager.getTransaction();
  *      transaction.begin();
  *     
  *      ... do work...
  *  
- *      transaction.commit(); 
+ *      transaction.commit();
  * 
* * @author Erik Brakkee @@ -64,9 +68,9 @@ import org.wamblee.test.persistence.JpaBuilder; public class SimpleTransactionManager { private UserTransactionFactory factory; - private ThreadLocal transaction; - private UserTransactionCallback callback; private List resources; + private UserTransactionCallback transactionFInishedCallback; + private ThreadSpecificProxyFactory transaction; /** * Constructs the transaction manager. @@ -76,15 +80,23 @@ public class SimpleTransactionManager { */ public SimpleTransactionManager(UserTransactionFactory aFactory) { factory = aFactory; - transaction = new ThreadLocal(); - 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); + } + }); } /** @@ -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(); } }