类 TransactionSynchronizationManager
- java.lang.Object
- org.springframework.transaction.reactive.TransactionSynchronizationManager
public class TransactionSynchronizationManager extends Object
Central delegate that manages resources and transaction synchronizations per subscriber context. 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 context-bound resources, e.g. 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()
andclearSynchronization()
. This is automatically supported byAbstractReactiveTransactionManager
, 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, e.g. a database connection for any given connection factory.
- 从以下版本开始:
- 5.2
- 作者:
- Mark Paluch, Juergen Hoeller
- 另请参阅:
isSynchronizationActive()
,registerSynchronization(org.springframework.transaction.reactive.TransactionSynchronization)
,TransactionSynchronization
构造器概要
构造器 构造器 说明 TransactionSynchronizationManager(TransactionContext transactionContext)
方法概要
所有方法 静态方法 实例方法 具体方法 修饰符和类型 方法 说明 void
bindResource(Object key, Object value)
Bind the given resource for the given key to the current context.void
clear()
Clear the entire transaction synchronization state: registered synchronizations as well as the various transaction characteristics.void
clearSynchronization()
Deactivate transaction synchronization for the current context.static reactor.core.publisher.Mono<TransactionSynchronizationManager>
forCurrentTransaction()
Get theTransactionSynchronizationManager
that is associated with the current transaction context.Integer
getCurrentTransactionIsolationLevel()
Return the isolation level for the current transaction, if any.String
getCurrentTransactionName()
Return the name of the current transaction, ornull
if none set.Object
getResource(Object key)
Retrieve a resource for the given key that is bound to the current thread.List<TransactionSynchronization>
getSynchronizations()
Return an unmodifiable snapshot list of all registered synchronizations for the current context.boolean
hasResource(Object key)
Check if there is a resource for the given key bound to the current thread.void
initSynchronization()
Activate transaction synchronization for the current context.boolean
isActualTransactionActive()
Return whether there currently is an actual transaction active.boolean
isCurrentTransactionReadOnly()
Return whether the current transaction is marked as read-only.boolean
isSynchronizationActive()
Return if transaction synchronization is active for the current context.void
registerSynchronization(TransactionSynchronization synchronization)
Register a new transaction synchronization for the current context.void
setActualTransactionActive(boolean active)
Expose whether there currently is an actual transaction active.void
setCurrentTransactionIsolationLevel(Integer isolationLevel)
Expose an isolation level for the current transaction.void
setCurrentTransactionName(String name)
Expose the name of the current transaction, if any.void
setCurrentTransactionReadOnly(boolean readOnly)
Expose a read-only flag for the current transaction.Object
unbindResource(Object key)
Unbind a resource for the given key from the current context.Object
unbindResourceIfPossible(Object key)
Unbind a resource for the given key from the current context.
构造器详细资料
TransactionSynchronizationManager
public TransactionSynchronizationManager(TransactionContext transactionContext)
方法详细资料
forCurrentTransaction
public static reactor.core.publisher.Mono<TransactionSynchronizationManager> forCurrentTransaction()
Get theTransactionSynchronizationManager
that is associated with the current transaction context.Mainly intended for code that wants to bind resources or synchronizations.
- 抛出:
NoTransactionException
- if the transaction info cannot be found — for example, because the method was invoked outside a managed transaction
hasResource
public boolean hasResource(Object key)
Check if there is a resource for the given key bound to the current thread.- 参数:
key
- the key to check (usually the resource factory)- 返回:
- if there is a value bound to the current thread
getResource
@Nullable public Object getResource(Object key)
Retrieve a resource for the given key that is bound to the current thread.- 参数:
key
- the key to check (usually the resource factory)- 返回:
- a value bound to the current thread (usually the active resource object), or
null
if none
bindResource
public void bindResource(Object key, Object value) throws IllegalStateException
Bind the given resource for the given key to the current context.- 参数:
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 context
unbindResource
public Object unbindResource(Object key) throws IllegalStateException
Unbind a resource for the given key from the current context.- 参数:
key
- the key to unbind (usually the resource factory)- 返回:
- the previously bound value (usually the active resource object)
- 抛出:
IllegalStateException
- if there is no value bound to the context
unbindResourceIfPossible
@Nullable public Object unbindResourceIfPossible(Object key)
Unbind a resource for the given key from the current context.- 参数:
key
- the key to unbind (usually the resource factory)- 返回:
- the previously bound value, or
null
if none bound
isSynchronizationActive
public boolean isSynchronizationActive()
Return if transaction synchronization is active for the current context. Can be called before register to avoid unnecessary instance creation.
initSynchronization
public void initSynchronization() throws IllegalStateException
Activate transaction synchronization for the current context. Called by a transaction manager on transaction begin.- 抛出:
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
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- 另请参阅:
Ordered
getSynchronizations
public List<TransactionSynchronization> getSynchronizations() throws IllegalStateException
Return an unmodifiable snapshot list of all registered synchronizations for the current context.- 返回:
- unmodifiable List of TransactionSynchronization instances
- 抛出:
IllegalStateException
- if synchronization is not active- 另请参阅:
TransactionSynchronization
clearSynchronization
public void clearSynchronization() throws IllegalStateException
Deactivate transaction synchronization for the current context. Called by the transaction manager on transaction cleanup.- 抛出:
IllegalStateException
- if synchronization is not active
setCurrentTransactionName
public void setCurrentTransactionName(@Nullable String name)
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, ornull
to reset it- 另请参阅:
TransactionDefinition.getName()
getCurrentTransactionName
@Nullable public String getCurrentTransactionName()
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.
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.- 参数:
readOnly
-true
to mark the current transaction as read-only;false
to reset such a read-only marker- 另请参阅:
TransactionDefinition.isReadOnly()
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
beforeCommit
callback, to be able to suppress change detection on commit. The present method is meant to be used for earlier read-only checks.
setCurrentTransactionIsolationLevel
public void setCurrentTransactionIsolationLevel(@Nullable Integer isolationLevel)
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 R2DBC Connection constants (equivalent to the corresponding Spring TransactionDefinition constants), ornull
to reset it- 另请参阅:
TransactionDefinition.ISOLATION_READ_UNCOMMITTED
,TransactionDefinition.ISOLATION_READ_COMMITTED
,TransactionDefinition.ISOLATION_REPEATABLE_READ
,TransactionDefinition.ISOLATION_SERIALIZABLE
,TransactionDefinition.getIsolationLevel()
getCurrentTransactionIsolationLevel
@Nullable public Integer 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).- 返回:
- the currently exposed isolation level, according to the R2DBC Connection constants (equivalent to the corresponding Spring TransactionDefinition constants), or
null
if none - 另请参阅:
TransactionDefinition.ISOLATION_READ_UNCOMMITTED
,TransactionDefinition.ISOLATION_READ_COMMITTED
,TransactionDefinition.ISOLATION_REPEATABLE_READ
,TransactionDefinition.ISOLATION_SERIALIZABLE
,TransactionDefinition.getIsolationLevel()
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.- 参数:
active
-true
to mark the current context as being associated with an actual transaction;false
to 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 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).
clear
public void clear()
Clear the entire transaction synchronization state: registered synchronizations as well as the various transaction characteristics.