001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.transaction.support;
018:
019: import java.util.ArrayList;
020: import java.util.Collections;
021: import java.util.Comparator;
022: import java.util.HashMap;
023: import java.util.LinkedList;
024: import java.util.List;
025: import java.util.Map;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029:
030: import org.springframework.core.OrderComparator;
031: import org.springframework.util.Assert;
032:
033: /**
034: * Central helper that manages resources and transaction synchronizations per thread.
035: * To be used by resource management code but not by typical application code.
036: *
037: * <p>Supports one resource per key without overwriting, that is, a resource needs
038: * to be removed before a new one can be set for the same key.
039: * Supports a list of transaction synchronizations if synchronization is active.
040: *
041: * <p>Resource management code should check for thread-bound resources, e.g. JDBC
042: * Connections or Hibernate Sessions, via <code>getResource</code>. Such code is
043: * normally not supposed to bind resources to threads, as this is the responsibility
044: * of transaction managers. A further option is to lazily bind on first use if
045: * transaction synchronization is active, for performing transactions that span
046: * an arbitrary number of resources.
047: *
048: * <p>Transaction synchronization must be activated and deactivated by a transaction
049: * manager via {@link #initSynchronization()} and {@link #clearSynchronization()}.
050: * This is automatically supported by {@link AbstractPlatformTransactionManager},
051: * and thus by all standard Spring transaction managers, such as
052: * {@link org.springframework.transaction.jta.JtaTransactionManager} and
053: * {@link org.springframework.jdbc.datasource.DataSourceTransactionManager}.
054: *
055: * <p>Resource management code should only register synchronizations when this
056: * manager is active, which can be checked via {@link #isSynchronizationActive};
057: * it should perform immediate resource cleanup else. If transaction synchronization
058: * isn't active, there is either no current transaction, or the transaction manager
059: * doesn't support transaction synchronization.
060: *
061: * <p>Synchronization is for example used to always return the same resources
062: * within a JTA transaction, e.g. a JDBC Connection or a Hibernate Session for
063: * any given DataSource or SessionFactory, respectively.
064: *
065: * @author Juergen Hoeller
066: * @since 02.06.2003
067: * @see #isSynchronizationActive
068: * @see #registerSynchronization
069: * @see TransactionSynchronization
070: * @see AbstractPlatformTransactionManager#setTransactionSynchronization
071: * @see org.springframework.transaction.jta.JtaTransactionManager
072: * @see org.springframework.jdbc.datasource.DataSourceTransactionManager
073: * @see org.springframework.jdbc.datasource.DataSourceUtils#getConnection
074: */
075: public abstract class TransactionSynchronizationManager {
076:
077: private static final Log logger = LogFactory
078: .getLog(TransactionSynchronizationManager.class);
079:
080: private static final ThreadLocal resources = new ThreadLocal();
081:
082: private static final ThreadLocal synchronizations = new ThreadLocal();
083:
084: private static final Comparator synchronizationComparator = new OrderComparator();
085:
086: private static final ThreadLocal currentTransactionName = new ThreadLocal();
087:
088: private static final ThreadLocal currentTransactionReadOnly = new ThreadLocal();
089:
090: private static final ThreadLocal currentTransactionIsolationLevel = new ThreadLocal();
091:
092: private static final ThreadLocal actualTransactionActive = new ThreadLocal();
093:
094: //-------------------------------------------------------------------------
095: // Management of transaction-associated resource handles
096: //-------------------------------------------------------------------------
097:
098: /**
099: * Return all resources that are bound to the current thread.
100: * <p>Mainly for debugging purposes. Resource managers should always invoke
101: * <code>hasResource</code> for a specific resource key that they are interested in.
102: * @return a Map with resource keys (usually the resource factory) and resource
103: * values (usually the active resource object), or an empty Map if there are
104: * currently no resources bound
105: * @see #hasResource
106: */
107: public static Map getResourceMap() {
108: Map map = (Map) resources.get();
109: return (map != null ? Collections.unmodifiableMap(map)
110: : Collections.EMPTY_MAP);
111: }
112:
113: /**
114: * Check if there is a resource for the given key bound to the current thread.
115: * @param key the key to check (usually the resource factory)
116: * @return if there is a value bound to the current thread
117: * @see ResourceTransactionManager#getResourceFactory()
118: */
119: public static boolean hasResource(Object key) {
120: Assert.notNull(key, "Key must not be null");
121: Map map = (Map) resources.get();
122: return (map != null && map.containsKey(key));
123: }
124:
125: /**
126: * Retrieve a resource for the given key that is bound to the current thread.
127: * @param key the key to check (usually the resource factory)
128: * @return a value bound to the current thread (usually the active
129: * resource object), or <code>null</code> if none
130: * @see ResourceTransactionManager#getResourceFactory()
131: */
132: public static Object getResource(Object key) {
133: Assert.notNull(key, "Key must not be null");
134: Map map = (Map) resources.get();
135: if (map == null) {
136: return null;
137: }
138: Object value = map.get(key);
139: if (value != null && logger.isDebugEnabled()) {
140: logger.debug("Retrieved value [" + value + "] for key ["
141: + key + "] bound to thread ["
142: + Thread.currentThread().getName() + "]");
143: }
144: return value;
145: }
146:
147: /**
148: * Bind the given resource for the given key to the current thread.
149: * @param key the key to bind the value to (usually the resource factory)
150: * @param value the value to bind (usually the active resource object)
151: * @throws IllegalStateException if there is already a value bound to the thread
152: * @see ResourceTransactionManager#getResourceFactory()
153: */
154: public static void bindResource(Object key, Object value)
155: throws IllegalStateException {
156: Assert.notNull(key, "Key must not be null");
157: Assert.notNull(value, "Value must not be null");
158: Map map = (Map) resources.get();
159: // set ThreadLocal Map if none found
160: if (map == null) {
161: map = new HashMap();
162: resources.set(map);
163: }
164: if (map.containsKey(key)) {
165: throw new IllegalStateException("Already value ["
166: + map.get(key) + "] for key [" + key
167: + "] bound to thread ["
168: + Thread.currentThread().getName() + "]");
169: }
170: map.put(key, value);
171: if (logger.isDebugEnabled()) {
172: logger.debug("Bound value [" + value + "] for key [" + key
173: + "] to thread ["
174: + Thread.currentThread().getName() + "]");
175: }
176: }
177:
178: /**
179: * Unbind a resource for the given key from the current thread.
180: * @param key the key to unbind (usually the resource factory)
181: * @return the previously bound value (usually the active resource object)
182: * @throws IllegalStateException if there is no value bound to the thread
183: * @see ResourceTransactionManager#getResourceFactory()
184: */
185: public static Object unbindResource(Object key)
186: throws IllegalStateException {
187: Assert.notNull(key, "Key must not be null");
188: Map map = (Map) resources.get();
189: if (map == null || !map.containsKey(key)) {
190: throw new IllegalStateException("No value for key [" + key
191: + "] bound to thread ["
192: + Thread.currentThread().getName() + "]");
193: }
194: Object value = map.remove(key);
195: // remove entire ThreadLocal if empty
196: if (map.isEmpty()) {
197: resources.set(null);
198: }
199: if (logger.isDebugEnabled()) {
200: logger.debug("Removed value [" + value + "] for key ["
201: + key + "] from thread ["
202: + Thread.currentThread().getName() + "]");
203: }
204: return value;
205: }
206:
207: //-------------------------------------------------------------------------
208: // Management of transaction synchronizations
209: //-------------------------------------------------------------------------
210:
211: /**
212: * Return if transaction synchronization is active for the current thread.
213: * Can be called before register to avoid unnecessary instance creation.
214: * @see #registerSynchronization
215: */
216: public static boolean isSynchronizationActive() {
217: return (synchronizations.get() != null);
218: }
219:
220: /**
221: * Activate transaction synchronization for the current thread.
222: * Called by a transaction manager on transaction begin.
223: * @throws IllegalStateException if synchronization is already active
224: */
225: public static void initSynchronization()
226: throws IllegalStateException {
227: if (isSynchronizationActive()) {
228: throw new IllegalStateException(
229: "Cannot activate transaction synchronization - already active");
230: }
231: logger.debug("Initializing transaction synchronization");
232: synchronizations.set(new LinkedList());
233: }
234:
235: /**
236: * Register a new transaction synchronization for the current thread.
237: * Typically called by resource management code.
238: * <p>Note that synchronizations can implement the
239: * {@link org.springframework.core.Ordered} interface.
240: * They will be executed in an order according to their order value (if any).
241: * @param synchronization the synchronization object to register
242: * @throws IllegalStateException if transaction synchronization is not active
243: * @see org.springframework.core.Ordered
244: */
245: public static void registerSynchronization(
246: TransactionSynchronization synchronization)
247: throws IllegalStateException {
248:
249: Assert.notNull(synchronization,
250: "TransactionSynchronization must not be null");
251: if (!isSynchronizationActive()) {
252: throw new IllegalStateException(
253: "Transaction synchronization is not active");
254: }
255: List synchs = (List) synchronizations.get();
256: synchs.add(synchronization);
257: }
258:
259: /**
260: * Return an unmodifiable snapshot list of all registered synchronizations
261: * for the current thread.
262: * @return unmodifiable List of TransactionSynchronization instances
263: * @throws IllegalStateException if synchronization is not active
264: * @see TransactionSynchronization
265: */
266: public static List getSynchronizations()
267: throws IllegalStateException {
268: if (!isSynchronizationActive()) {
269: throw new IllegalStateException(
270: "Transaction synchronization is not active");
271: }
272: List synchs = (List) synchronizations.get();
273: // Sort lazily here, not in registerSynchronization.
274: Collections.sort(synchs, synchronizationComparator);
275: // Return unmodifiable snapshot, to avoid ConcurrentModificationExceptions
276: // while iterating and invoking synchronization callbacks that in turn
277: // might register further synchronizations.
278: return Collections.unmodifiableList(new ArrayList(synchs));
279: }
280:
281: /**
282: * Deactivate transaction synchronization for the current thread.
283: * Called by the transaction manager on transaction cleanup.
284: * @throws IllegalStateException if synchronization is not active
285: */
286: public static void clearSynchronization()
287: throws IllegalStateException {
288: if (!isSynchronizationActive()) {
289: throw new IllegalStateException(
290: "Cannot deactivate transaction synchronization - not active");
291: }
292: logger.debug("Clearing transaction synchronization");
293: synchronizations.set(null);
294: }
295:
296: //-------------------------------------------------------------------------
297: // Exposure of transaction characteristics
298: //-------------------------------------------------------------------------
299:
300: /**
301: * Expose the name of the current transaction, if any.
302: * Called by the transaction manager on transaction begin and on cleanup.
303: * @param name the name of the transaction, or <code>null</code> to reset it
304: * @see org.springframework.transaction.TransactionDefinition#getName()
305: */
306: public static void setCurrentTransactionName(String name) {
307: currentTransactionName.set(name);
308: }
309:
310: /**
311: * Return the name of the current transaction, or <code>null</code> if none set.
312: * To be called by resource management code for optimizations per use case,
313: * for example to optimize fetch strategies for specific named transactions.
314: * @see org.springframework.transaction.TransactionDefinition#getName()
315: */
316: public static String getCurrentTransactionName() {
317: return (String) currentTransactionName.get();
318: }
319:
320: /**
321: * Expose a read-only flag for the current transaction.
322: * Called by the transaction manager on transaction begin and on cleanup.
323: * @param readOnly <code>true</code> to mark the current transaction
324: * as read-only; <code>false</code> to reset such a read-only marker
325: * @see org.springframework.transaction.TransactionDefinition#isReadOnly()
326: */
327: public static void setCurrentTransactionReadOnly(boolean readOnly) {
328: currentTransactionReadOnly.set(readOnly ? Boolean.TRUE : null);
329: }
330:
331: /**
332: * Return whether the current transaction is marked as read-only.
333: * To be called by resource management code when preparing a newly
334: * created resource (for example, a Hibernate Session).
335: * <p>Note that transaction synchronizations receive the read-only flag
336: * as argument for the <code>beforeCommit</code> callback, to be able
337: * to suppress change detection on commit. The present method is meant
338: * to be used for earlier read-only checks, for example to set the
339: * flush mode of a Hibernate Session to "FlushMode.NEVER" upfront.
340: * @see org.springframework.transaction.TransactionDefinition#isReadOnly()
341: * @see TransactionSynchronization#beforeCommit(boolean)
342: * @see org.hibernate.Session#flush
343: * @see org.hibernate.Session#setFlushMode
344: * @see org.hibernate.FlushMode#NEVER
345: */
346: public static boolean isCurrentTransactionReadOnly() {
347: return (currentTransactionReadOnly.get() != null);
348: }
349:
350: /**
351: * Expose an isolation level for the current transaction.
352: * Called by the transaction manager on transaction begin and on cleanup.
353: * @param isolationLevel the isolation level to expose, according to the
354: * JDBC Connection constants (equivalent to the corresponding Spring
355: * TransactionDefinition constants), or <code>null</code> to reset it
356: * @see java.sql.Connection#TRANSACTION_READ_UNCOMMITTED
357: * @see java.sql.Connection#TRANSACTION_READ_COMMITTED
358: * @see java.sql.Connection#TRANSACTION_REPEATABLE_READ
359: * @see java.sql.Connection#TRANSACTION_SERIALIZABLE
360: * @see org.springframework.transaction.TransactionDefinition#ISOLATION_READ_UNCOMMITTED
361: * @see org.springframework.transaction.TransactionDefinition#ISOLATION_READ_COMMITTED
362: * @see org.springframework.transaction.TransactionDefinition#ISOLATION_REPEATABLE_READ
363: * @see org.springframework.transaction.TransactionDefinition#ISOLATION_SERIALIZABLE
364: * @see org.springframework.transaction.TransactionDefinition#getIsolationLevel()
365: */
366: public static void setCurrentTransactionIsolationLevel(
367: Integer isolationLevel) {
368: currentTransactionIsolationLevel.set(isolationLevel);
369: }
370:
371: /**
372: * Return the isolation level for the current transaction, if any.
373: * To be called by resource management code when preparing a newly
374: * created resource (for example, a JDBC Connection).
375: * @return the currently exposed isolation level, according to the
376: * JDBC Connection constants (equivalent to the corresponding Spring
377: * TransactionDefinition constants), or <code>null</code> if none
378: * @see java.sql.Connection#TRANSACTION_READ_UNCOMMITTED
379: * @see java.sql.Connection#TRANSACTION_READ_COMMITTED
380: * @see java.sql.Connection#TRANSACTION_REPEATABLE_READ
381: * @see java.sql.Connection#TRANSACTION_SERIALIZABLE
382: * @see org.springframework.transaction.TransactionDefinition#ISOLATION_READ_UNCOMMITTED
383: * @see org.springframework.transaction.TransactionDefinition#ISOLATION_READ_COMMITTED
384: * @see org.springframework.transaction.TransactionDefinition#ISOLATION_REPEATABLE_READ
385: * @see org.springframework.transaction.TransactionDefinition#ISOLATION_SERIALIZABLE
386: * @see org.springframework.transaction.TransactionDefinition#getIsolationLevel()
387: */
388: public static Integer getCurrentTransactionIsolationLevel() {
389: return (Integer) currentTransactionIsolationLevel.get();
390: }
391:
392: /**
393: * Expose whether there currently is an actual transaction active.
394: * Called by the transaction manager on transaction begin and on cleanup.
395: * @param active <code>true</code> to mark the current thread as being associated
396: * with an actual transaction; <code>false</code> to reset that marker
397: */
398: public static void setActualTransactionActive(boolean active) {
399: actualTransactionActive.set(active ? Boolean.TRUE : null);
400: }
401:
402: /**
403: * Return whether there currently is an actual transaction active.
404: * This indicates whether the current thread is associated with an actual
405: * transaction rather than just with active transaction synchronization.
406: * <p>To be called by resource management code that wants to discriminate
407: * between active transaction synchronization (with or without backing
408: * resource transaction; also on PROPAGATION_SUPPORTS) and an actual
409: * transaction being active (with backing resource transaction;
410: * on PROPAGATION_REQUIRES, PROPAGATION_REQUIRES_NEW, etc).
411: * @see #isSynchronizationActive()
412: */
413: public static boolean isActualTransactionActive() {
414: return (actualTransactionActive.get() != null);
415: }
416:
417: /**
418: * Clear the entire transaction synchronization state for the current thread:
419: * registered synchronizations as well as the various transaction characteristics.
420: * @see #clearSynchronization()
421: * @see #setCurrentTransactionName
422: * @see #setCurrentTransactionReadOnly
423: * @see #setCurrentTransactionIsolationLevel
424: * @see #setActualTransactionActive
425: */
426: public static void clear() {
427: clearSynchronization();
428: setCurrentTransactionName(null);
429: setCurrentTransactionReadOnly(false);
430: setCurrentTransactionIsolationLevel(null);
431: setActualTransactionActive(false);
432: }
433:
434: }
|