Class TransactionSynchronizationManager
Supports one resource per key without overwriting, that is, a resource needs to be removed before a new one can be set for the same key. Supports a list of transaction synchronizations if synchronization is active.
Resource management code should check for thread-bound resources, e.g. JDBC
 Connections or Hibernate Sessions, via getResource. Such code is
 normally not supposed to bind resources to threads, as this is the responsibility
 of transaction managers. A further option is to lazily bind on first use if
 transaction synchronization is active, for performing transactions that span
 an arbitrary number of resources.
 
Transaction synchronization must be activated and deactivated by a transaction
 manager via initSynchronization() and clearSynchronization().
 This is automatically supported by AbstractPlatformTransactionManager,
 and thus by all standard Spring transaction managers, such as
 JtaTransactionManager and
 DataSourceTransactionManager.
 
Resource management code should only register synchronizations when this
 manager is active, which can be checked via isSynchronizationActive();
 it should perform immediate resource cleanup else. If transaction synchronization
 isn't active, there is either no current transaction, or the transaction manager
 doesn't support transaction synchronization.
 
Synchronization is for example used to always return the same resources within a JTA transaction, e.g. a JDBC Connection or a Hibernate Session for any given DataSource or SessionFactory, respectively.
- Since:
- 02.06.2003
- Author:
- Juergen Hoeller
- See Also:
- 
- isSynchronizationActive()
- registerSynchronization(org.springframework.transaction.support.TransactionSynchronization)
- TransactionSynchronization
- AbstractPlatformTransactionManager.setTransactionSynchronization(int)
- JtaTransactionManager
- DataSourceTransactionManager
- DataSourceUtils.getConnection(javax.sql.DataSource)
 
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionstatic voidbindResource(Object key, Object value) Bind the given resource for the given key to the current thread.static voidclear()Clear the entire transaction synchronization state for the current thread: registered synchronizations as well as the various transaction characteristics.static voidDeactivate transaction synchronization for the current thread.static IntegerReturn the isolation level for the current transaction, if any.static StringReturn the name of the current transaction, ornullif none set.static ObjectgetResource(Object key) Retrieve a resource for the given key that is bound to the current thread.Return all resources that are bound to the current thread.static List<TransactionSynchronization>Return an unmodifiable snapshot list of all registered synchronizations for the current thread.static booleanhasResource(Object key) Check if there is a resource for the given key bound to the current thread.static voidActivate transaction synchronization for the current thread.static booleanReturn whether there currently is an actual transaction active.static booleanReturn whether the current transaction is marked as read-only.static booleanReturn if transaction synchronization is active for the current thread.static voidregisterSynchronization(TransactionSynchronization synchronization) Register a new transaction synchronization for the current thread.static voidsetActualTransactionActive(boolean active) Expose whether there currently is an actual transaction active.static voidsetCurrentTransactionIsolationLevel(Integer isolationLevel) Expose an isolation level for the current transaction.static voidExpose the name of the current transaction, if any.static voidsetCurrentTransactionReadOnly(boolean readOnly) Expose a read-only flag for the current transaction.static ObjectunbindResource(Object key) Unbind a resource for the given key from the current thread.static ObjectUnbind a resource for the given key from the current thread.
- 
Constructor Details- 
TransactionSynchronizationManagerpublic TransactionSynchronizationManager()
 
- 
- 
Method Details- 
getResourceMapReturn all resources that are bound to the current thread.Mainly for debugging purposes. Resource managers should always invoke hasResourcefor a specific resource key that they are interested in.- Returns:
- a Map with resource keys (usually the resource factory) and resource values (usually the active resource object), or an empty Map if there are currently no resources bound
- See Also:
 
- 
hasResourceCheck if there is a resource for the given key bound to the current thread.- Parameters:
- key- the key to check (usually the resource factory)
- Returns:
- if there is a value bound to the current thread
- See Also:
 
- 
getResourceRetrieve a resource for the given key that is bound to the current thread.- Parameters:
- key- the key to check (usually the resource factory)
- Returns:
- a value bound to the current thread (usually the active
 resource object), or nullif none
- See Also:
 
- 
bindResourceBind the given resource for the given key to the current thread.- Parameters:
- key- the key to bind the value to (usually the resource factory)
- value- the value to bind (usually the active resource object)
- Throws:
- IllegalStateException- if there is already a value bound to the thread
- See Also:
 
- 
unbindResourceUnbind a resource for the given key from the current thread.- Parameters:
- key- the key to unbind (usually the resource factory)
- Returns:
- the previously bound value (usually the active resource object)
- Throws:
- IllegalStateException- if there is no value bound to the thread
- See Also:
 
- 
unbindResourceIfPossibleUnbind a resource for the given key from the current thread.- Parameters:
- key- the key to unbind (usually the resource factory)
- Returns:
- the previously bound value, or nullif none bound
 
- 
isSynchronizationActivepublic static boolean isSynchronizationActive()Return if transaction synchronization is active for the current thread. Can be called before register to avoid unnecessary instance creation.
- 
initSynchronizationActivate transaction synchronization for the current thread. Called by a transaction manager on transaction begin.- Throws:
- IllegalStateException- if synchronization is already active
 
- 
registerSynchronizationpublic static void registerSynchronization(TransactionSynchronization synchronization) throws IllegalStateException Register a new transaction synchronization for the current thread. Typically called by resource management code.Note that synchronizations can implement the Orderedinterface. They will be executed in an order according to their order value (if any).- Parameters:
- synchronization- the synchronization object to register
- Throws:
- IllegalStateException- if transaction synchronization is not active
- See Also:
 
- 
getSynchronizationsReturn an unmodifiable snapshot list of all registered synchronizations for the current thread.- Returns:
- unmodifiable List of TransactionSynchronization instances
- Throws:
- IllegalStateException- if synchronization is not active
- See Also:
 
- 
clearSynchronizationDeactivate transaction synchronization for the current thread. Called by the transaction manager on transaction cleanup.- Throws:
- IllegalStateException- if synchronization is not active
 
- 
setCurrentTransactionNameExpose the name of the current transaction, if any. Called by the transaction manager on transaction begin and on cleanup.- Parameters:
- name- the name of the transaction, or- nullto reset it
- See Also:
 
- 
getCurrentTransactionNameReturn the name of the current transaction, ornullif none set. To be called by resource management code for optimizations per use case, for example to optimize fetch strategies for specific named transactions.- See Also:
 
- 
setCurrentTransactionReadOnlypublic static void setCurrentTransactionReadOnly(boolean readOnly) Expose a read-only flag for the current transaction. Called by the transaction manager on transaction begin and on cleanup.- Parameters:
- readOnly-- trueto mark the current transaction as read-only;- falseto reset such a read-only marker
- See Also:
 
- 
isCurrentTransactionReadOnlypublic static boolean isCurrentTransactionReadOnly()Return whether the current transaction is marked as read-only. To be called by resource management code when preparing a newly created resource (for example, a Hibernate Session).Note that transaction synchronizations receive the read-only flag as argument for the beforeCommitcallback, to be able to suppress change detection on commit. The present method is meant to be used for earlier read-only checks, for example to set the flush mode of a Hibernate Session to "FlushMode.MANUAL" upfront.
- 
setCurrentTransactionIsolationLevelExpose an isolation level for the current transaction. Called by the transaction manager on transaction begin and on cleanup.- Parameters:
- isolationLevel- the isolation level to expose, according to the JDBC Connection constants (equivalent to the corresponding Spring TransactionDefinition constants), or- nullto reset it
- See Also:
- 
- Connection.TRANSACTION_READ_UNCOMMITTED
- Connection.TRANSACTION_READ_COMMITTED
- Connection.TRANSACTION_REPEATABLE_READ
- Connection.TRANSACTION_SERIALIZABLE
- TransactionDefinition.ISOLATION_READ_UNCOMMITTED
- TransactionDefinition.ISOLATION_READ_COMMITTED
- TransactionDefinition.ISOLATION_REPEATABLE_READ
- TransactionDefinition.ISOLATION_SERIALIZABLE
- TransactionDefinition.getIsolationLevel()
 
 
- 
getCurrentTransactionIsolationLevelReturn the isolation level for the current transaction, if any. To be called by resource management code when preparing a newly created resource (for example, a JDBC Connection).- Returns:
- the currently exposed isolation level, according to the
 JDBC Connection constants (equivalent to the corresponding Spring
 TransactionDefinition constants), or nullif none
- See Also:
- 
- Connection.TRANSACTION_READ_UNCOMMITTED
- Connection.TRANSACTION_READ_COMMITTED
- Connection.TRANSACTION_REPEATABLE_READ
- Connection.TRANSACTION_SERIALIZABLE
- TransactionDefinition.ISOLATION_READ_UNCOMMITTED
- TransactionDefinition.ISOLATION_READ_COMMITTED
- TransactionDefinition.ISOLATION_REPEATABLE_READ
- TransactionDefinition.ISOLATION_SERIALIZABLE
- TransactionDefinition.getIsolationLevel()
 
 
- 
setActualTransactionActivepublic static void setActualTransactionActive(boolean active) Expose whether there currently is an actual transaction active. Called by the transaction manager on transaction begin and on cleanup.- Parameters:
- active-- trueto mark the current thread as being associated with an actual transaction;- falseto reset that marker
 
- 
isActualTransactionActivepublic static boolean isActualTransactionActive()Return whether there currently is an actual transaction active. This indicates whether the current thread is associated with an actual transaction rather than just with active transaction synchronization.To be called by resource management code that wants to differentiate between active transaction synchronization (with or without a backing resource transaction; also on PROPAGATION_SUPPORTS) and an actual transaction being active (with a backing resource transaction; on PROPAGATION_REQUIRED, PROPAGATION_REQUIRES_NEW, etc). - See Also:
 
- 
clearpublic static void clear()Clear the entire transaction synchronization state for the current thread: registered synchronizations as well as the various transaction characteristics.
 
-