(no commit message)
authorErik Brakkee <erik@brakkee.org>
Thu, 22 Jul 2010 17:29:56 +0000 (17:29 +0000)
committerErik Brakkee <erik@brakkee.org>
Thu, 22 Jul 2010 17:29:56 +0000 (17:29 +0000)
test/enterprise/src/main/java/org/wamblee/test/transactions/AutoCreateUserTransaction.java [new file with mode: 0644]
test/enterprise/src/main/java/org/wamblee/test/transactions/SimpleTransactionManager.java
test/enterprise/src/main/java/org/wamblee/test/transactions/SimpleUserTransaction.java

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
new file mode 100644 (file)
index 0000000..7b5124b
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ * 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);   
+    }
+
+}
index 81a5bfad36f3479957d42a16a2c67aae6301fab8..3ba4e8147c760e19d76a410e25f4ec1d3f1f0224 100644 (file)
@@ -64,7 +64,7 @@ import org.wamblee.test.persistence.JpaBuilder;
 public class SimpleTransactionManager {
 
     private UserTransactionFactory factory;
-    private ThreadSpecificProxyFactory<UserTransaction> transaction; 
+    private ThreadLocal<UserTransaction> transaction; 
     private UserTransactionCallback callback;
     private List<TransactionResource> resources;
 
@@ -76,7 +76,7 @@ public class SimpleTransactionManager {
      */
     public SimpleTransactionManager(UserTransactionFactory aFactory) {
         factory = aFactory;
-        transaction = new ThreadSpecificProxyFactory<UserTransaction>(UserTransaction.class);
+        transaction = new ThreadLocal<UserTransaction>();
         callback = new UserTransactionCallback() {
 
             @Override
@@ -106,12 +106,16 @@ public class SimpleTransactionManager {
      * @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 transaction.getProxy();
+        return tx;
     }
     
     /**
index 1b9b59ac5a125194373230006bef11eb5c2f1e58..ffa8925a34ef0f9479b18636c8c2b43b8b86d033 100644 (file)
@@ -34,7 +34,7 @@ public class SimpleUserTransaction implements UserTransaction {
     private int status;
 
     /**
-     * Construcst the transaction.
+     * Constructs the transaction.
      * 
      * @param aCallback
      *            Callback to use.