Class TransactionSynchronizationManager
- java.lang.Object
-
- io.micronaut.transaction.support.TransactionSynchronizationManager
-
public abstract class TransactionSynchronizationManager extends java.lang.Object
Central delegate 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()
andclearSynchronization()
. This is automatically supported byAbstractSynchronousTransactionManager
, 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 Classes Modifier and Type Class Description static interface
TransactionSynchronizationManager.TransactionSynchronizationState
The synchronization state.static interface
TransactionSynchronizationManager.TransactionSynchronizationStateOp
The transaction synchronization state autocloseable operation.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.Object
DEFAULT_STATE_KEY
-
Constructor Summary
Constructors Constructor Description TransactionSynchronizationManager()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static void
bindResource(java.lang.Object key, java.lang.Object value)
Bind the given resource for the given key to the current thread.static void
bindSynchronousTransactionState(java.lang.Object key, SynchronousTransactionState state)
static void
clear()
Deprecated.static void
clearSynchronization()
Deprecated.static <T> java.util.concurrent.CompletionStage<T>
decorateCompletionStage(TransactionSynchronizationManager.TransactionSynchronizationState state, java.util.function.Supplier<java.util.concurrent.CompletionStage<T>> supplier)
Decorate completion stage supplier with the transaction state.static <T> java.util.function.Supplier<T>
decorateToPropagateState(java.util.function.Supplier<T> supplier)
Decorate the supplier with possible propagated state in thread-local.static TransactionDefinition.Isolation
getCurrentTransactionIsolationLevel()
Deprecated.static java.lang.String
getCurrentTransactionName()
Deprecated.static TransactionSynchronizationManager.TransactionSynchronizationState
getOrCreateState()
Get existing or create new associated state.static SynchronousTransactionState
getRequiredSynchronousTransactionState(java.lang.Object key)
static java.lang.Object
getResource(java.lang.Object key)
Retrieve a resource for the given key that is bound to the current thread.static java.util.Map<java.lang.Object,java.lang.Object>
getResourceMap()
Return all resources that are bound to the current thread.static <T> T
getResourceOrDefault(java.lang.Object key, T defaultValue)
static TransactionSynchronizationManager.TransactionSynchronizationState
getState()
Get the existing state.static java.util.List<TransactionSynchronization>
getSynchronizations()
Deprecated.static SynchronousTransactionState
getSynchronousTransactionState(java.lang.Object key)
static SynchronousTransactionState
getSynchronousTransactionStateOrCreate(java.lang.Object key, java.util.function.Supplier<SynchronousTransactionState> creator)
static boolean
hasResource(java.lang.Object key)
Check if there is a resource for the given key bound to the current thread.static void
initSynchronization()
Deprecated.static boolean
isActualTransactionActive()
Deprecated.static boolean
isCurrentTransactionReadOnly()
Deprecated.static boolean
isSynchronizationActive()
Deprecated.static void
rebindResource(java.lang.Object key, java.lang.Object value)
static void
registerSynchronization(TransactionSynchronization synchronization)
Deprecated.static void
setActualTransactionActive(boolean active)
Deprecated.static void
setCurrentTransactionIsolationLevel(TransactionDefinition.Isolation isolationLevel)
Deprecated.static void
setCurrentTransactionName(java.lang.String name)
Deprecated.static void
setCurrentTransactionReadOnly(boolean readOnly)
Deprecated.static void
setState(TransactionSynchronizationManager.TransactionSynchronizationState state)
Restore the state from the thread local.static java.lang.Object
unbindResource(java.lang.Object key)
Unbind a resource for the given key from the current thread.static java.lang.Object
unbindResourceIfPossible(java.lang.Object key)
Unbind a resource for the given key from the current thread.static SynchronousTransactionState
unbindSynchronousTransactionState(java.lang.Object key)
static TransactionSynchronizationManager.TransactionSynchronizationStateOp
withGuardedState()
Execute provided supplier with setup state in the thread-local.static TransactionSynchronizationManager.TransactionSynchronizationStateOp
withState(TransactionSynchronizationManager.TransactionSynchronizationState state)
Execute provided supplier with setup state in the thread-local.static <T> T
withState(TransactionSynchronizationManager.TransactionSynchronizationState state, java.util.function.Supplier<T> supplier)
Execute provided supplier with setup state in the thread-local.
-
-
-
Method Detail
-
getResourceMap
public static java.util.Map<java.lang.Object,java.lang.Object> 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(java.lang.Object)
-
hasResource
public static boolean hasResource(java.lang.Object key)
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:
ResourceTransactionManager.getResourceFactory()
-
getResource
@Nullable public static java.lang.Object getResource(java.lang.Object key)
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:
ResourceTransactionManager.getResourceFactory()
-
getResourceOrDefault
@NonNull public static <T> T getResourceOrDefault(java.lang.Object key, @NonNull T defaultValue)
-
bindResource
public static void bindResource(java.lang.Object key, java.lang.Object value) throws java.lang.IllegalStateException
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:
java.lang.IllegalStateException
- if there is already a value bound to the thread- See Also:
ResourceTransactionManager.getResourceFactory()
-
rebindResource
public static void rebindResource(java.lang.Object key, java.lang.Object value) throws java.lang.IllegalStateException
- Throws:
java.lang.IllegalStateException
-
unbindResource
public static java.lang.Object unbindResource(java.lang.Object key) throws java.lang.IllegalStateException
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:
java.lang.IllegalStateException
- if there is no value bound to the thread- See Also:
ResourceTransactionManager.getResourceFactory()
-
unbindResourceIfPossible
@Nullable public static java.lang.Object unbindResourceIfPossible(java.lang.Object key)
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 java.lang.Object key, @NonNull SynchronousTransactionState state)
-
unbindSynchronousTransactionState
public static SynchronousTransactionState unbindSynchronousTransactionState(java.lang.Object key) throws java.lang.IllegalStateException
- Throws:
java.lang.IllegalStateException
-
getSynchronousTransactionState
@Nullable public static SynchronousTransactionState getSynchronousTransactionState(@NonNull java.lang.Object key)
-
getRequiredSynchronousTransactionState
@NonNull public static SynchronousTransactionState getRequiredSynchronousTransactionState(@NonNull java.lang.Object key)
-
getSynchronousTransactionStateOrCreate
@NonNull public static SynchronousTransactionState getSynchronousTransactionStateOrCreate(@NonNull java.lang.Object key, java.util.function.Supplier<SynchronousTransactionState> creator)
-
isSynchronizationActive
@Deprecated public static boolean 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(io.micronaut.transaction.support.TransactionSynchronization)
-
initSynchronization
@Deprecated public static void initSynchronization() throws java.lang.IllegalStateException
Deprecated.Activate transaction synchronization for the current thread. Called by a transaction manager on transaction begin.- Throws:
java.lang.IllegalStateException
- if synchronization is already active
-
registerSynchronization
@Deprecated public static void registerSynchronization(TransactionSynchronization synchronization) throws java.lang.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:
java.lang.IllegalStateException
- if transaction synchronization is not active- See Also:
Ordered
-
getSynchronizations
@Deprecated public static java.util.List<TransactionSynchronization> getSynchronizations() throws java.lang.IllegalStateException
Deprecated.Return an unmodifiable snapshot list of all registered synchronizations for the current thread.- Returns:
- unmodifiable List of TransactionSynchronization instances
- Throws:
java.lang.IllegalStateException
- if synchronization is not active- See Also:
TransactionSynchronization
-
clearSynchronization
@Deprecated public static void clearSynchronization() throws java.lang.IllegalStateException
Deprecated.Deactivate transaction synchronization for the current thread. Called by the transaction manager on transaction cleanup.- Throws:
java.lang.IllegalStateException
- if synchronization is not active
-
setCurrentTransactionName
@Deprecated public static void setCurrentTransactionName(@Nullable java.lang.String name)
Deprecated.Expose 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, ornull
to reset it- See Also:
TransactionDefinition.getName()
-
getCurrentTransactionName
@Deprecated @Nullable public static java.lang.String getCurrentTransactionName()
Deprecated.Return the name of the current transaction, ornull
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.- Returns:
- The current transaction name
- See Also:
TransactionDefinition.getName()
-
setCurrentTransactionReadOnly
@Deprecated public static void setCurrentTransactionReadOnly(boolean readOnly)
Deprecated.Expose a read-only flag for the current transaction. Called by the transaction manager on transaction begin and on cleanup.- Parameters:
readOnly
-true
to mark the current transaction as read-only;false
to reset such a read-only marker- See Also:
TransactionDefinition.isReadOnly()
-
isCurrentTransactionReadOnly
@Deprecated public static boolean isCurrentTransactionReadOnly()
Deprecated.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.- Returns:
- Whether the transaction is read only
- See Also:
TransactionDefinition.isReadOnly()
,TransactionSynchronization.beforeCommit(boolean)
-
setCurrentTransactionIsolationLevel
@Deprecated public static void setCurrentTransactionIsolationLevel(@Nullable TransactionDefinition.Isolation isolationLevel)
Deprecated.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 JDBC Connection constants (equivalent to the corresponding TransactionDefinition constants), ornull
to 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()
-
getCurrentTransactionIsolationLevel
@Nullable @Deprecated public static TransactionDefinition.Isolation getCurrentTransactionIsolationLevel()
Deprecated.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).- Returns:
- the currently exposed isolation level, according to the
JDBC Connection constants (equivalent to the corresponding
TransactionDefinition constants), or
null
if 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()
-
setActualTransactionActive
@Deprecated public static void setActualTransactionActive(boolean active)
Deprecated.Expose whether there currently is an actual transaction active. Called by the transaction manager on transaction begin and on cleanup.- Parameters:
active
-true
to mark the current thread as being associated with an actual transaction;false
to reset that marker
-
isActualTransactionActive
@Deprecated public static boolean isActualTransactionActive()
Deprecated.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_REQUIRED, PROPAGATION_REQUIRES_NEW, etc).
- Returns:
- Whether a transaction is active
- See Also:
isSynchronizationActive()
-
clear
@Deprecated public static void clear()
Deprecated.Clear the entire transaction synchronization state for the current thread: registered synchronizations as well as the various transaction characteristics.
-
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, java.util.function.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> java.util.concurrent.CompletionStage<T> decorateCompletionStage(@Nullable TransactionSynchronizationManager.TransactionSynchronizationState state, java.util.function.Supplier<java.util.concurrent.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
@Internal public static <T> java.util.function.Supplier<T> decorateToPropagateState(java.util.function.Supplier<T> supplier)
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
-
-