KickJava   Java API By Example, From Geeks To Geeks.

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


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
22 /**
23  * Default implementation of the {@link org.springframework.transaction.TransactionStatus}
24  * interface, used by {@link AbstractPlatformTransactionManager}. Based on the concept
25  * of an underlying "transaction object".
26  *
27  * <p>Holds all status information that {@link AbstractPlatformTransactionManager}
28  * needs internally, including a generic transaction object determined by the
29  * concrete transaction manager implementation.
30  *
31  * <p>Supports delegating savepoint-related methods to a transaction object
32  * that implements the {@link SavepointManager} interface.
33  *
34  * <p><b>NOTE:</b> This is <i>not</i> intended to be used for other
35  * PlatformTransactionManager implementations, in particular not for
36  * mock transaction managers. Use {@link SimpleTransactionStatus} or
37  * a mock for the plain TransactionStatus interface instead.
38  *
39  * @author Juergen Hoeller
40  * @since 19.01.2004
41  * @see AbstractPlatformTransactionManager
42  * @see org.springframework.transaction.SavepointManager
43  * @see #getTransaction
44  * @see #createSavepoint
45  * @see #rollbackToSavepoint
46  * @see #releaseSavepoint
47  * @see SimpleTransactionStatus
48  */

49 public class DefaultTransactionStatus extends AbstractTransactionStatus {
50
51     private final Object JavaDoc transaction;
52
53     private final boolean newTransaction;
54
55     private final boolean newSynchronization;
56
57     private final boolean readOnly;
58
59     private final boolean debug;
60
61     private final Object JavaDoc suspendedResources;
62
63
64     /**
65      * Create a new DefaultTransactionStatus instance.
66      * @param transaction underlying transaction object that can hold
67      * state for the internal transaction implementation
68      * @param newTransaction if the transaction is new,
69      * else participating in an existing transaction
70      * @param newSynchronization if a new transaction synchronization
71      * has been opened for the given transaction
72      * @param readOnly whether the transaction is read-only
73      * @param debug should debug logging be enabled for the handling of this transaction?
74      * Caching it in here can prevent repeated calls to ask the logging system whether
75      * debug logging should be enabled.
76      * @param suspendedResources a holder for resources that have been suspended
77      * for this transaction, if any
78      */

79     public DefaultTransactionStatus(
80         Object JavaDoc transaction, boolean newTransaction, boolean newSynchronization,
81             boolean readOnly, boolean debug, Object JavaDoc suspendedResources) {
82
83         this.transaction = transaction;
84         this.newTransaction = newTransaction;
85         this.newSynchronization = newSynchronization;
86         this.readOnly = readOnly;
87         this.debug = debug;
88         this.suspendedResources = suspendedResources;
89     }
90
91     /**
92      * Return the underlying transaction object.
93      */

94     public Object JavaDoc getTransaction() {
95         return this.transaction;
96     }
97
98     /**
99      * Return whether there is an actual transaction active.
100      */

101     public boolean hasTransaction() {
102         return (this.transaction != null);
103     }
104
105     public boolean isNewTransaction() {
106         return (hasTransaction() && this.newTransaction);
107     }
108
109     /**
110      * Return if a new transaction synchronization has been opened
111      * for this transaction.
112      */

113     public boolean isNewSynchronization() {
114         return this.newSynchronization;
115     }
116
117     /**
118      * Return if this transaction is defined as read-only transaction.
119      */

120     public boolean isReadOnly() {
121         return this.readOnly;
122     }
123
124     /**
125      * Return whether the progress of this transaction is debugged. This is used
126      * by AbstractPlatformTransactionManager as an optimization, to prevent repeated
127      * calls to logger.isDebug(). Not really intended for client code.
128      */

129     public boolean isDebug() {
130         return this.debug;
131     }
132
133     /**
134      * Return the holder for resources that have been suspended for this transaction,
135      * if any.
136      */

137     public Object JavaDoc getSuspendedResources() {
138         return this.suspendedResources;
139     }
140
141
142     //---------------------------------------------------------------------
143
// Enable functionality through underlying transaction object
144
//---------------------------------------------------------------------
145

146     /**
147      * Determine the rollback-only flag via checking both the transaction object,
148      * provided that the latter implements the SmartTransactionObject interface.
149      * <p>Will return "true" if the transaction itself has been marked rollback-only
150      * by the transaction coordinator, for example in case of a timeout.
151      * @see SmartTransactionObject#isRollbackOnly
152      */

153     public boolean isGlobalRollbackOnly() {
154         return ((this.transaction instanceof SmartTransactionObject) &&
155                 ((SmartTransactionObject) this.transaction).isRollbackOnly());
156     }
157
158     /**
159      * This implementation exposes the SavepointManager interface
160      * of the underlying transaction object, if any.
161      */

162     protected SavepointManager getSavepointManager() {
163         if (!isTransactionSavepointManager()) {
164             throw new NestedTransactionNotSupportedException(
165                 "Transaction object [" + getTransaction() + "] does not support savepoints");
166         }
167         return (SavepointManager) getTransaction();
168     }
169
170     /**
171      * Return whether the underlying transaction implements the
172      * SavepointManager interface.
173      * @see #getTransaction
174      * @see org.springframework.transaction.SavepointManager
175      */

176     public boolean isTransactionSavepointManager() {
177         return (getTransaction() instanceof SavepointManager);
178     }
179
180 }
181
Popular Tags