KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > transaction > support > AbstractTransactionStatus


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */

16
17 package org.springframework.transaction.support;
18
19 import org.springframework.transaction.NestedTransactionNotSupportedException;
20 import org.springframework.transaction.SavepointManager;
21 import org.springframework.transaction.TransactionException;
22 import org.springframework.transaction.TransactionStatus;
23 import org.springframework.transaction.TransactionUsageException;
24
25 /**
26  * Abstract base implementation of the
27  * {@link org.springframework.transaction.TransactionStatus} interface.
28  *
29  * <p>Pre-implements the handling of local rollback-only and completed flags, and
30  * delegation to an underlying {@link org.springframework.transaction.SavepointManager}.
31  * Also offers the option of a holding a savepoint within the transaction.
32  *
33  * <p>Does not assume any specific internal transaction handling, such as an
34  * underlying transaction object, and no transaction synchronization mechanism.
35  *
36  * @author Juergen Hoeller
37  * @since 1.2.3
38  * @see #setRollbackOnly()
39  * @see #isRollbackOnly()
40  * @see #setCompleted()
41  * @see #isCompleted()
42  * @see #getSavepointManager()
43  * @see SimpleTransactionStatus
44  * @see DefaultTransactionStatus
45  */

46 public abstract class AbstractTransactionStatus implements TransactionStatus {
47
48     private boolean rollbackOnly = false;
49
50     private boolean completed = false;
51
52     private Object JavaDoc savepoint;
53
54
55     //---------------------------------------------------------------------
56
// Handling of current transaction state
57
//---------------------------------------------------------------------
58

59     public void setRollbackOnly() {
60         this.rollbackOnly = true;
61     }
62
63     /**
64      * Determine the rollback-only flag via checking both the local rollback-only flag
65      * of this TransactionStatus and the global rollback-only flag of the underlying
66      * transaction, if any.
67      * @see #isLocalRollbackOnly()
68      * @see #isGlobalRollbackOnly()
69      */

70     public boolean isRollbackOnly() {
71         return (isLocalRollbackOnly() || isGlobalRollbackOnly());
72     }
73
74     /**
75      * Determine the rollback-only flag via checking this TransactionStatus.
76      * <p>Will only return "true" if the application called <code>setRollbackOnly</code>
77      * on this TransactionStatus object.
78      */

79     public boolean isLocalRollbackOnly() {
80         return this.rollbackOnly;
81     }
82
83     /**
84      * Template method for determining the global rollback-only flag of the
85      * underlying transaction, if any.
86      * <p>This implementation always returns <code>false</code>.
87      */

88     public boolean isGlobalRollbackOnly() {
89         return false;
90     }
91
92     /**
93      * Mark this transaction as completed, that is, committed or rolled back.
94      */

95     public void setCompleted() {
96         this.completed = true;
97     }
98
99     public boolean isCompleted() {
100         return this.completed;
101     }
102
103
104     //---------------------------------------------------------------------
105
// Handling of current savepoint state
106
//---------------------------------------------------------------------
107

108     /**
109      * Set a savepoint for this transaction. Useful for PROPAGATION_NESTED.
110      * @see org.springframework.transaction.TransactionDefinition#PROPAGATION_NESTED
111      */

112     protected void setSavepoint(Object JavaDoc savepoint) {
113         this.savepoint = savepoint;
114     }
115
116     /**
117      * Get the savepoint for this transaction, if any.
118      */

119     protected Object JavaDoc getSavepoint() {
120         return this.savepoint;
121     }
122
123     public boolean hasSavepoint() {
124         return (this.savepoint != null);
125     }
126
127     /**
128      * Create a savepoint and hold it for the transaction.
129      * @throws org.springframework.transaction.NestedTransactionNotSupportedException
130      * if the underlying transaction does not support savepoints
131      */

132     public void createAndHoldSavepoint() throws TransactionException {
133         setSavepoint(getSavepointManager().createSavepoint());
134     }
135
136     /**
137      * Roll back to the savepoint that is held for the transaction.
138      */

139     public void rollbackToHeldSavepoint() throws TransactionException {
140         if (!hasSavepoint()) {
141             throw new TransactionUsageException("No savepoint associated with current transaction");
142         }
143         getSavepointManager().rollbackToSavepoint(getSavepoint());
144         setSavepoint(null);
145     }
146
147     /**
148      * Release the savepoint that is held for the transaction.
149      */

150     public void releaseHeldSavepoint() throws TransactionException {
151         if (!hasSavepoint()) {
152             throw new TransactionUsageException("No savepoint associated with current transaction");
153         }
154         getSavepointManager().releaseSavepoint(getSavepoint());
155         setSavepoint(null);
156     }
157
158
159     //---------------------------------------------------------------------
160
// Implementation of SavepointManager
161
//---------------------------------------------------------------------
162

163     /**
164      * This implementation delegates to a SavepointManager for the
165      * underlying transaction, if possible.
166      * @see #getSavepointManager()
167      * @see org.springframework.transaction.SavepointManager
168      */

169     public Object JavaDoc createSavepoint() throws TransactionException {
170         return getSavepointManager().createSavepoint();
171     }
172
173     /**
174      * This implementation delegates to a SavepointManager for the
175      * underlying transaction, if possible.
176      * @throws org.springframework.transaction.NestedTransactionNotSupportedException
177      * @see #getSavepointManager()
178      * @see org.springframework.transaction.SavepointManager
179      */

180     public void rollbackToSavepoint(Object JavaDoc savepoint) throws TransactionException {
181         getSavepointManager().rollbackToSavepoint(savepoint);
182     }
183
184     /**
185      * This implementation delegates to a SavepointManager for the
186      * underlying transaction, if possible.
187      * @see #getSavepointManager()
188      * @see org.springframework.transaction.SavepointManager
189      */

190     public void releaseSavepoint(Object JavaDoc savepoint) throws TransactionException {
191         getSavepointManager().releaseSavepoint(savepoint);
192     }
193
194     /**
195      * Return a SavepointManager for the underlying transaction, if possible.
196      * <p>Default implementation always throws a NestedTransactionNotSupportedException.
197      * @throws org.springframework.transaction.NestedTransactionNotSupportedException
198      * if the underlying transaction does not support savepoints
199      */

200     protected SavepointManager getSavepointManager() {
201         throw new NestedTransactionNotSupportedException("This transaction does not support savepoints");
202     }
203
204 }
205
Popular Tags