java.lang.Object | |
↳ | org.springframework.transaction.support.TransactionSynchronizationManager |
Central helper that manages resources and transaction synchronizations per thread. To be used by resource management code but not by typical application code.
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.
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Bind the given resource for the given key to the current thread.
| |||||||||||
Clear the entire transaction synchronization state for the current thread:
registered synchronizations as well as the various transaction characteristics.
| |||||||||||
Deactivate transaction synchronization for the current thread.
| |||||||||||
Return the isolation level for the current transaction, if any.
| |||||||||||
Return the name of the current transaction, or
null if none set. | |||||||||||
Retrieve a resource for the given key that is bound to the current thread.
| |||||||||||
Return all resources that are bound to the current thread.
| |||||||||||
Return an unmodifiable snapshot list of all registered synchronizations
for the current thread.
| |||||||||||
Check if there is a resource for the given key bound to the current thread.
| |||||||||||
Activate transaction synchronization for the current thread.
| |||||||||||
Return whether there currently is an actual transaction active.
| |||||||||||
Return whether the current transaction is marked as read-only.
| |||||||||||
Return if transaction synchronization is active for the current thread.
| |||||||||||
Register a new transaction synchronization for the current thread.
| |||||||||||
Expose whether there currently is an actual transaction active.
| |||||||||||
Expose an isolation level for the current transaction.
| |||||||||||
Expose the name of the current transaction, if any.
| |||||||||||
Expose a read-only flag for the current transaction.
| |||||||||||
Unbind a resource for the given key from the current thread.
| |||||||||||
Unbind a resource for the given key from the current thread.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
|
Bind the given resource for the given key to the current thread.
key | the key to bind the value to (usually the resource factory) |
---|---|
value | the value to bind (usually the active resource object) |
IllegalStateException | if there is already a value bound to the thread |
---|
Clear the entire transaction synchronization state for the current thread: registered synchronizations as well as the various transaction characteristics.
Deactivate transaction synchronization for the current thread. Called by the transaction manager on transaction cleanup.
IllegalStateException | if synchronization is not active |
---|
Return 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).
null
if noneReturn the name of the current transaction, or null
if none set.
To be called by resource management code for optimizations per use case,
for example to optimize fetch strategies for specific named transactions.
Retrieve a resource for the given key that is bound to the current thread.
key | the key to check (usually the resource factory) |
---|
null
if noneReturn all resources that are bound to the current thread.
Mainly for debugging purposes. Resource managers should always invoke
hasResource
for a specific resource key that they are interested in.
Return an unmodifiable snapshot list of all registered synchronizations for the current thread.
IllegalStateException | if synchronization is not active |
---|
Check if there is a resource for the given key bound to the current thread.
key | the key to check (usually the resource factory) |
---|
Activate transaction synchronization for the current thread. Called by a transaction manager on transaction begin.
IllegalStateException | if synchronization is already active |
---|
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 discriminate between active transaction synchronization (with or without backing resource transaction; also on PROPAGATION_SUPPORTS) and an actual transaction being active (with backing resource transaction; on PROPAGATION_REQUIRES, PROPAGATION_REQUIRES_NEW, etc).
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 beforeCommit
callback, 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.NEVER" upfront.
Return if transaction synchronization is active for the current thread. Can be called before register to avoid unnecessary instance creation.
Register a new transaction synchronization for the current thread. Typically called by resource management code.
Note that synchronizations can implement the
Ordered
interface.
They will be executed in an order according to their order value (if any).
synchronization | the synchronization object to register |
---|
IllegalStateException | if transaction synchronization is not active |
---|
Expose whether there currently is an actual transaction active. Called by the transaction manager on transaction begin and on cleanup.
active | true to mark the current thread as being associated
with an actual transaction; false to reset that marker
|
---|
Expose an isolation level for the current transaction. Called by the transaction manager on transaction begin and on cleanup.
isolationLevel | the isolation level to expose, according to the
JDBC Connection constants (equivalent to the corresponding Spring
TransactionDefinition constants), or null to reset it |
---|
Expose the name of the current transaction, if any. Called by the transaction manager on transaction begin and on cleanup.
name | the name of the transaction, or null to reset it |
---|
Expose a read-only flag for the current transaction. Called by the transaction manager on transaction begin and on cleanup.
readOnly | true to mark the current transaction
as read-only; false to reset such a read-only marker |
---|
Unbind a resource for the given key from the current thread.
key | the key to unbind (usually the resource factory) |
---|
IllegalStateException | if there is no value bound to the thread |
---|
Unbind a resource for the given key from the current thread.
key | the key to unbind (usually the resource factory) |
---|
null
if none bound