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 AbstractSynchronousTransactionManager
,
and thus by all standard 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 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(io.micronaut.transaction.support.TransactionSynchronization)
TransactionSynchronization
AbstractSynchronousStateTransactionManager.setTransactionSynchronization(io.micronaut.transaction.support.AbstractSynchronousStateTransactionManager.Synchronization)
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
The synchronization state.static interface
The transaction synchronization state autocloseable operation. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
bindResource
(Object key, Object value) Bind the given resource for the given key to the current thread.static void
static <T> CompletionStage<T>
decorateCompletionStage
(TransactionSynchronizationManager.TransactionSynchronizationState state, Supplier<CompletionStage<T>> supplier) Decorate completion stage supplier with the transaction state.static <T> Supplier<T>
decorateToPropagateState
(Supplier<T> supplier) Decorate the supplier with possible propagated state in thread-local.Get existing or create new associated state.static SynchronousTransactionState
static Object
getResource
(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 <T> T
getResourceOrDefault
(Object key, T defaultValue) getState()
Get the existing state.static SynchronousTransactionState
static SynchronousTransactionState
static boolean
hasResource
(Object key) Check if there is a resource for the given key bound to the current thread.static boolean
Deprecated.static void
rebindResource
(Object key, Object value) static void
registerSynchronization
(TransactionSynchronization synchronization) Deprecated.static void
Restore the state from the thread local.static Object
unbindResource
(Object key) Unbind a resource for the given key from the current thread.static Object
Unbind a resource for the given key from the current thread.static SynchronousTransactionState
Execute provided supplier with setup state in the thread-local.Execute provided supplier with setup state in the thread-local.static <T> T
withState
(TransactionSynchronizationManager.TransactionSynchronizationState state, Supplier<T> supplier) Execute provided supplier with setup state in the thread-local.
-
Field Details
-
DEFAULT_STATE_KEY
-
-
Constructor Details
-
TransactionSynchronizationManager
public TransactionSynchronizationManager()
-
-
Method Details
-
getResourceMap
Return 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.- 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:
-
hasResource
Check 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:
-
getResource
Retrieve 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
null
if none - See Also:
-
getResourceOrDefault
-
bindResource
Bind 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:
-
rebindResource
- Throws:
IllegalStateException
-
unbindResource
Unbind 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:
-
unbindResourceIfPossible
Unbind 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
null
if none bound
-
bindSynchronousTransactionState
public static void bindSynchronousTransactionState(@NonNull Object key, @NonNull SynchronousTransactionState state) -
unbindSynchronousTransactionState
public static SynchronousTransactionState unbindSynchronousTransactionState(Object key) throws IllegalStateException - Throws:
IllegalStateException
-
getSynchronousTransactionState
@Nullable public static SynchronousTransactionState getSynchronousTransactionState(@NonNull Object key) -
getRequiredSynchronousTransactionState
@NonNull public static SynchronousTransactionState getRequiredSynchronousTransactionState(@NonNull Object key) -
getSynchronousTransactionStateOrCreate
@NonNull public static SynchronousTransactionState getSynchronousTransactionStateOrCreate(@NonNull Object key, Supplier<SynchronousTransactionState> creator) -
isSynchronizationActive
Deprecated.Return if transaction synchronization is active for the current thread. Can be called before register to avoid unnecessary instance creation.- Returns:
- True if a synchronization is active
- See Also:
-
registerSynchronization
@Deprecated public static void registerSynchronization(TransactionSynchronization synchronization) throws IllegalStateException Deprecated.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).- Parameters:
synchronization
- the synchronization object to register- Throws:
IllegalStateException
- if transaction synchronization is not active- See Also:
-
Ordered
-
getState
@Internal @Nullable public static TransactionSynchronizationManager.TransactionSynchronizationState getState()Get the existing state.- Returns:
- The state
- Since:
- 3.3
-
getOrCreateState
@Internal @NonNull public static TransactionSynchronizationManager.TransactionSynchronizationState getOrCreateState()Get existing or create new associated state.- Returns:
- The state
- Since:
- 3.3
-
setState
@Internal public static void setState(@Nullable TransactionSynchronizationManager.TransactionSynchronizationState state) Restore the state from the thread local.- Parameters:
state
- The state- Since:
- 3.3
-
withState
@Internal public static <T> T withState(@Nullable TransactionSynchronizationManager.TransactionSynchronizationState state, Supplier<T> supplier) Execute provided supplier with setup state in the thread-local. Afterwards update the state with the modifications done to it.- Type Parameters:
T
- The supplied type- Parameters:
state
- The statesupplier
- The supplier to be executed- Returns:
- The suppler return value
- Since:
- 3.4.0
-
decorateCompletionStage
@Internal public static <T> CompletionStage<T> decorateCompletionStage(@Nullable TransactionSynchronizationManager.TransactionSynchronizationState state, Supplier<CompletionStage<T>> supplier) Decorate completion stage supplier with the transaction state.- Type Parameters:
T
- The supplied type- Parameters:
state
- The statesupplier
- The supplier to be executed- Returns:
- The suppler return value
- Since:
- 3.5.0
-
withState
@Internal public static TransactionSynchronizationManager.TransactionSynchronizationStateOp withState(@Nullable TransactionSynchronizationManager.TransactionSynchronizationState state) Execute provided supplier with setup state in the thread-local. Afterwards update the state with the modifications done to it.- Parameters:
state
- The state- Returns:
- The suppler return value
- Since:
- 3.5.0
-
withGuardedState
@Internal public static TransactionSynchronizationManager.TransactionSynchronizationStateOp withGuardedState()Execute provided supplier with setup state in the thread-local. Make sure the thread-local state is cleanup afterwards.- Returns:
- 3.5.0
-
decorateToPropagateState
Decorate the supplier with possible propagated state in thread-local.It's used to propagated TX state down to the async functions.
- Type Parameters:
T
- The supplied type- Parameters:
supplier
- The supplier to be decorated- Returns:
- The decorated supplier
- Since:
- 3.4.0
-
getSynchronousTransactionState(Object)