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.general.ThreadSpecificProxyFactory.CreationCallback;
25 import org.wamblee.test.persistence.JpaBuilder;
30 * Simple transaction manager provides a simple mechanism to manage transactions
31 * in test code through the {@link UserTransaction} object. To construct the
32 * transaction manager use:
36 * SimpleTransactionManager manager = new SimpleTransactionManager(
37 * new DefaultUserTransactionFactory());
41 * Next, add resources to manage using {@link #addResource(TransactionResource)}:
44 * manager.addResource(jpaTester.getJpaBuilder());
47 * As you can see from the above, {@link JpaBuilder} is a resource that can be
48 * used so this mechanism can be used with JPA testing.
52 * The next step is to manage transactions using the standard
53 * <code>UserTransaction</code> APIs:
57 * UserTransaction transaction = manager.getTransaction();
58 * transaction.begin();
62 * transaction.commit();
65 * @author Erik Brakkee
68 public class SimpleTransactionManager {
70 private UserTransactionFactory factory;
71 private List<TransactionResource> resources;
72 private UserTransactionCallback transactionFInishedCallback;
73 private ThreadSpecificProxyFactory<UserTransaction> transaction;
76 * Constructs the transaction manager.
79 * Factory to create transactions with.
81 public SimpleTransactionManager(UserTransactionFactory aFactory) {
84 resources = new ArrayList<TransactionResource>();
86 transactionFInishedCallback = new UserTransactionCallback() {
88 public void transactionFinished() {
89 transaction.set(factory.create(this, resources));
92 transaction = new ThreadSpecificProxyFactory<UserTransaction>(
93 UserTransaction.class, new CreationCallback<UserTransaction>() {
95 public UserTransaction create() {
96 return factory.create(transactionFInishedCallback,
103 * Adds a resource to manage. Adding resources is no longer allowed after
104 * the first transaction has started.
109 public void addResource(TransactionResource aResource) {
110 resources.add(aResource);
114 * Gets the user transaction. This is a contextual reference, meaning that
115 * it will delegate to the appropriate thread-specific user transaction. It
116 * is also safe to store in a JNDI tree and for caching by applications.
118 * @return User transaction.
120 public UserTransaction getTransaction() {
121 return transaction.getProxy();
125 * Gets the thread-specific transaction object.
127 * @return Transaction object.
129 UserTransaction getThreadSpecificTransaction() {
130 getTransaction(); // create tx if needed
131 return transaction.get();