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 javax.transaction.HeuristicMixedException;
19 import javax.transaction.HeuristicRollbackException;
20 import javax.transaction.NotSupportedException;
21 import javax.transaction.RollbackException;
22 import javax.transaction.Status;
23 import javax.transaction.SystemException;
24 import javax.transaction.UserTransaction;
27 * Represents a user transaction spanning a number of resources.
29 public class SimpleUserTransaction implements UserTransaction {
31 private UserTransactionCallback callback;
32 private TransactionResource[] resources;
33 private Object[] txStates;
37 * Constructs the transaction.
44 public SimpleUserTransaction(UserTransactionCallback aCallback,
45 TransactionResource<?>... aResources) {
47 resources = aResources;
48 txStates = new Object[resources.length];
49 status = Status.STATUS_NO_TRANSACTION;
53 public void begin() throws NotSupportedException, SystemException {
54 if (status == Status.STATUS_ACTIVE) {
55 throw new NotSupportedException(
56 "Nested transactions not supported!");
58 for (int i = 0; i < resources.length; i++) {
59 txStates[i] = resources[i].begin();
61 status = Status.STATUS_ACTIVE;
65 public void rollback() throws IllegalStateException, SecurityException,
67 if ( status == Status.STATUS_NO_TRANSACTION) {
68 throw new IllegalStateException("Rollback while not in a transaction");
71 for (int i = 0; i < resources.length; i++) {
72 resources[i].rollback(txStates[i]);
75 status = Status.STATUS_NO_TRANSACTION;
76 callback.transactionFinished();
81 public void commit() throws RollbackException, HeuristicMixedException,
82 HeuristicRollbackException, SecurityException, IllegalStateException,
84 if (status == Status.STATUS_MARKED_ROLLBACK) {
86 throw new RollbackException();
88 if (status != Status.STATUS_ACTIVE) {
89 throw new IllegalStateException("Commit while not in a transaction");
92 boolean committing = true;
93 for (int i = 0; i < resources.length; i++) {
96 resources[i].commit(txStates[i]);
98 resources[i].rollback(txStates[i]);
100 } catch (Exception e) {
106 throw new HeuristicMixedException("Commit failed");
109 status = Status.STATUS_NO_TRANSACTION;
110 callback.transactionFinished();
115 public int getStatus() throws SystemException {
120 public void setRollbackOnly() throws IllegalStateException, SystemException {
121 if ( status == Status.STATUS_NO_TRANSACTION) {
122 throw new IllegalStateException("setRollbackOnly() while not in a transaction");
124 status = Status.STATUS_MARKED_ROLLBACK;
128 public void setTransactionTimeout(int aSeconds) throws SystemException {