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 context-bound resources, for example,
database connections, via getResource. Such code is normally not
supposed to bind resources to units of work, 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 AbstractReactiveTransactionManager,
and thus by all standard Spring transaction managers.
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 transaction, for example, a database connection for any given connection factory.
- Since:
- 5.2
- Author:
- Mark Paluch, Juergen Hoeller
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidbindResource(Object key, Object value) Bind the given resource for the given key to the current context.voidclear()Clear the entire transaction synchronization state: registered synchronizations as well as the various transaction characteristics.voidDeactivate transaction synchronization for the current context.static reactor.core.publisher.Mono<TransactionSynchronizationManager> Get theTransactionSynchronizationManagerthat is associated with the current transaction context.Return the isolation level for the current transaction, if any.Return the name of the current transaction, ornullif none set.getResource(Object key) Retrieve a resource for the given key that is bound to the current context.Return an unmodifiable snapshot list of all registered synchronizations for the current context.booleanhasResource(Object key) Check if there is a resource for the given key bound to the current context.voidActivate transaction synchronization for the current context.booleanReturn whether there currently is an actual transaction active.booleanReturn whether the current transaction is marked as read-only.booleanReturn if transaction synchronization is active for the current context.voidregisterSynchronization(TransactionSynchronization synchronization) Register a new transaction synchronization for the current context.voidsetActualTransactionActive(boolean active) Expose whether there currently is an actual transaction active.voidsetCurrentTransactionIsolationLevel(@Nullable Integer isolationLevel) Expose an isolation level for the current transaction.voidExpose the name of the current transaction, if any.voidsetCurrentTransactionReadOnly(boolean readOnly) Expose a read-only flag for the current transaction.unbindResource(Object key) Unbind a resource for the given key from the current context.Unbind a resource for the given key from the current context.
-
Constructor Details
-
TransactionSynchronizationManager
-
-
Method Details
-
forCurrentTransaction
public static reactor.core.publisher.Mono<TransactionSynchronizationManager> forCurrentTransaction()Get theTransactionSynchronizationManagerthat is associated with the current transaction context.Mainly intended for code that wants to bind resources or synchronizations.
- Throws:
NoTransactionException- if the transaction info cannot be found — for example, because the method was invoked outside a managed transaction
-
hasResource
Check if there is a resource for the given key bound to the current context.- Parameters:
key- the key to check (usually the resource factory)- Returns:
- if there is a value bound to the current context
-
getResource
-
bindResource
Bind the given resource for the given key to the current context.- 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 context
-
unbindResource
Unbind a resource for the given key from the current context.- 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 context
-
unbindResourceIfPossible
-
isSynchronizationActive
public boolean isSynchronizationActive()Return if transaction synchronization is active for the current context. Can be called before register to avoid unnecessary instance creation.- See Also:
-
initSynchronization
Activate transaction synchronization for the current context. Called by a transaction manager on transaction begin.- Throws:
IllegalStateException- if synchronization is already active
-
registerSynchronization
public void registerSynchronization(TransactionSynchronization synchronization) throws IllegalStateException Register a new transaction synchronization for the current context. 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:
-
getSynchronizations
Return an unmodifiable snapshot list of all registered synchronizations for the current context.- Returns:
- unmodifiable List of TransactionSynchronization instances
- Throws:
IllegalStateException- if synchronization is not active- See Also:
-
clearSynchronization
Deactivate transaction synchronization for the current context. Called by the transaction manager on transaction cleanup.- Throws:
IllegalStateException- if synchronization is not active
-
setCurrentTransactionName
-
getCurrentTransactionName
-
setCurrentTransactionReadOnly
public 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:
-
isCurrentTransactionReadOnly
public 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.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.- See Also:
-
setCurrentTransactionIsolationLevel
Expose 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 R2DBC Connection constants (equivalent to the corresponding Spring TransactionDefinition constants), ornullto reset it- See Also:
-
getCurrentTransactionIsolationLevel
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 R2DBC Connection).- Returns:
- the currently exposed isolation level, according to the
R2DBC Connection constants (equivalent to the corresponding Spring
TransactionDefinition constants), or
nullif none - See Also:
-
setActualTransactionActive
public 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 context as being associated with an actual transaction;falseto reset that marker
-
isActualTransactionActive
public boolean isActualTransactionActive()Return whether there currently is an actual transaction active. This indicates whether the current context 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:
-
clear
public void clear()Clear the entire transaction synchronization state: registered synchronizations as well as the various transaction characteristics.- See Also:
-