2 * Copyright 2005-2010 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.test.transactions;
18 import java.util.ArrayList;
19 import java.util.List;
21 import javax.transaction.UserTransaction;
23 import org.wamblee.general.ThreadSpecificProxyFactory;
24 import org.wamblee.test.persistence.JpaBuilder;
29 * Simple transaction manager provides a simple mechanism to manage transactions
30 * in test code through the {@link UserTransaction} object.
31 * To construct the transaction manager use:
34 * SimpleTransactionManager manager =
35 * new SimpleTransactionManager(new DefaultUserTransactionFactory());
39 * Next, add resources to manage using {@link #addResource(TransactionResource)}:
41 * manager.addResource(jpaTester.getJpaBuilder());
44 * As you can see from the above, {@link JpaBuilder} is a resource that can be used
45 * so this mechanism can be used with JPA testing.
49 * The next step is to manage transactions using the standard <code>UserTransaction</code>
53 * UserTransaction transaction = manager.getTransaction();
54 * transaction.begin();
58 * transaction.commit();
61 * @author Erik Brakkee
64 public class SimpleTransactionManager {
66 private UserTransactionFactory factory;
67 private ThreadSpecificProxyFactory<UserTransaction> transaction;
68 private UserTransactionCallback callback;
69 private List<TransactionResource> resources;
72 * Constructs the transaction manager.
75 * Factory to create transactions with.
77 public SimpleTransactionManager(UserTransactionFactory aFactory) {
79 transaction = new ThreadSpecificProxyFactory<UserTransaction>(UserTransaction.class);
80 callback = new UserTransactionCallback() {
83 public void transactionFinished() {
84 transaction.set(null);
87 resources = new ArrayList<TransactionResource>();
91 * Adds a resource to manage. Adding resources is no longer allowed after
92 * the first transaction has started.
97 public void addResource(TransactionResource aResource) {
98 resources.add(aResource);
102 * Gets the user transaction. This is a contextual reference, meaning that
103 * it will delegate to the appropriate thread-specific user transaction.
104 * It is also safe to store in a JNDI tree and for caching by applications.
106 * @return User transaction.
108 public UserTransaction getTransaction() {
109 UserTransaction tx = transaction.get();
111 tx = factory.create(callback, resources);
114 return transaction.getProxy();
118 * Gets the thread-specific transaction object.
119 * @return Transaction object.
121 UserTransaction getThreadSpecificTransaction() {
122 getTransaction(); // create tx if needed
123 return transaction.get();