Top Description Fields Constructors Methods
org.springframework.transaction.reactive

public Class TransactionSynchronizationManager

extends Object
Class Inheritance
Imports
java.util.ArrayList, .Collections, .LinkedHashSet, .List, .Map, .Set, reactor.core.publisher.Mono, org.springframework.core.annotation.AnnotationAwareOrderComparator, org.springframework.lang.Nullable, org.springframework.transaction.NoTransactionException, org.springframework.util.Assert

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() 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, e.g. a database connection for any given connection factory.

Authors
Mark Paluch, Juergen Hoeller
Since
5.2
See Also
isSynchronizationActive, registerSynchronization, TransactionSynchronization

Field Summary

Modifier and TypeField and Description
private final TransactionContext

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
bindResource(Object
the key to bind the value to (usually the resource factory)
key
,
Object
the value to bind (usually the active resource object)
value
)

Bind the given resource for the given key to the current context.

public void
clear()

Clear the entire transaction synchronization state: registered synchronizations as well as the various transaction characteristics.

public void
clearSynchronization()

Deactivate transaction synchronization for the current context.

private Object
doGetResource(Object actualKey)

Actually check the value of the resource that is bound for the given key.

private Object
doUnbindResource(Object actualKey)

Actually remove the value of the resource that is bound for the given key.

public static Mono<TransactionSynchronizationManager>
forCurrentTransaction()

Get the TransactionSynchronizationManager that is associated with the current transaction context.

public Integer

Returns:

the currently exposed isolation level, according to the R2DBC Connection constants (equivalent to the corresponding Spring TransactionDefinition constants), or null if none
getCurrentTransactionIsolationLevel
()

Return the isolation level for the current transaction, if any.

public String
getCurrentTransactionName()

Return the name of the current transaction, or null if none set.

public Object

Returns:

a value bound to the current context (usually the active resource object), or null if none
getResource
(Object
the key to check (usually the resource factory)
key
)

Retrieve a resource for the given key that is bound to the current context.

public List<TransactionSynchronization>

Returns:

unmodifiable List of TransactionSynchronization instances
getSynchronizations
()

Return an unmodifiable snapshot list of all registered synchronizations for the current context.

public boolean

Returns:

if there is a value bound to the current context
hasResource
(Object
the key to check (usually the resource factory)
key
)

Check if there is a resource for the given key bound to the current context.

public void
initSynchronization()

Activate transaction synchronization for the current context.

public boolean
isActualTransactionActive()

Return whether there currently is an actual transaction active.

public boolean
isCurrentTransactionReadOnly()

Return whether the current transaction is marked as read-only.

public boolean
isSynchronizationActive()

Return if transaction synchronization is active for the current context.

public void
registerSynchronization(TransactionSynchronization
the synchronization object to register
synchronization
)

Register a new transaction synchronization for the current context.

public void
setActualTransactionActive(boolean
true to mark the current context as being associated with an actual transaction; false to reset that marker
active
)

Expose whether there currently is an actual transaction active.

public void
setCurrentTransactionIsolationLevel(Integer
the isolation level to expose, according to the R2DBC Connection constants (equivalent to the corresponding Spring TransactionDefinition constants), or null to reset it
isolationLevel
)

Expose an isolation level for the current transaction.

public void
setCurrentTransactionName(String
the name of the transaction, or null to reset it
name
)

Expose the name of the current transaction, if any.

public void
setCurrentTransactionReadOnly(boolean
true to mark the current transaction as read-only; false to reset such a read-only marker
readOnly
)

Expose a read-only flag for the current transaction.

public Object

Returns:

the previously bound value (usually the active resource object)
unbindResource
(Object
the key to unbind (usually the resource factory)
key
)

Unbind a resource for the given key from the current context.

public Object

Returns:

the previously bound value, or null if none bound
unbindResourceIfPossible
(Object
the key to unbind (usually the resource factory)
key
)

Unbind a resource for the given key from the current context.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

transactionContextback to summary
private final TransactionContext transactionContext

Constructor Detail

TransactionSynchronizationManagerback to summary
public TransactionSynchronizationManager(TransactionContext transactionContext)

Method Detail

bindResourceback to summary
public void bindResource(Object key, Object value) throws IllegalStateException

Bind the given resource for the given key to the current context.

Parameters
key:Object

the key to bind the value to (usually the resource factory)

value:Object

the value to bind (usually the active resource object)

Exceptions
IllegalStateException:
if there is already a value bound to the context
clearback to summary
public void clear()

Clear the entire transaction synchronization state: registered synchronizations as well as the various transaction characteristics.

See Also
clearSynchronization(), setCurrentTransactionName, setCurrentTransactionReadOnly, setCurrentTransactionIsolationLevel, setActualTransactionActive
clearSynchronizationback to summary
public void clearSynchronization() throws IllegalStateException

Deactivate transaction synchronization for the current context. Called by the transaction manager on transaction cleanup.

Exceptions
IllegalStateException:
if synchronization is not active
doGetResourceback to summary
private Object doGetResource(Object actualKey)

Actually check the value of the resource that is bound for the given key.

Annotations
@Nullable
doUnbindResourceback to summary
private Object doUnbindResource(Object actualKey)

Actually remove the value of the resource that is bound for the given key.

Annotations
@Nullable
forCurrentTransactionback to summary
public static Mono<TransactionSynchronizationManager> forCurrentTransaction()

Get the TransactionSynchronizationManager that is associated with the current transaction context.

Mainly intended for code that wants to bind resources or synchronizations.

Exceptions
NoTransactionException:
if the transaction info cannot be found — for example, because the method was invoked outside a managed transaction
getCurrentTransactionIsolationLevelback to summary
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).

Returns:Integer

the currently exposed isolation level, according to the R2DBC Connection constants (equivalent to the corresponding Spring TransactionDefinition constants), or null if none

Annotations
@Nullable
See Also
org.springframework.transaction.TransactionDefinition#ISOLATION_READ_UNCOMMITTED, org.springframework.transaction.TransactionDefinition#ISOLATION_READ_COMMITTED, org.springframework.transaction.TransactionDefinition#ISOLATION_REPEATABLE_READ, org.springframework.transaction.TransactionDefinition#ISOLATION_SERIALIZABLE, org.springframework.transaction.TransactionDefinition#getIsolationLevel()
getCurrentTransactionNameback to summary
public String getCurrentTransactionName()

Return 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.

Annotations
@Nullable
See Also
org.springframework.transaction.TransactionDefinition#getName()
getResourceback to summary
public Object getResource(Object key)

Retrieve a resource for the given key that is bound to the current context.

Parameters
key:Object

the key to check (usually the resource factory)

Returns:Object

a value bound to the current context (usually the active resource object), or null if none

Annotations
@Nullable
getSynchronizationsback to summary
public List<TransactionSynchronization> getSynchronizations() throws IllegalStateException

Return an unmodifiable snapshot list of all registered synchronizations for the current context.

Returns:List<TransactionSynchronization>

unmodifiable List of TransactionSynchronization instances

Exceptions
IllegalStateException:
if synchronization is not active
See Also
TransactionSynchronization
hasResourceback to summary
public boolean hasResource(Object key)

Check if there is a resource for the given key bound to the current context.

Parameters
key:Object

the key to check (usually the resource factory)

Returns:boolean

if there is a value bound to the current context

initSynchronizationback to summary
public void initSynchronization() throws IllegalStateException

Activate transaction synchronization for the current context. Called by a transaction manager on transaction begin.

Exceptions
IllegalStateException:
if synchronization is already active
isActualTransactionActiveback to summary
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
isSynchronizationActive()
isCurrentTransactionReadOnlyback to summary
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.

See Also
org.springframework.transaction.TransactionDefinition#isReadOnly(), TransactionSynchronization#beforeCommit(boolean)
isSynchronizationActiveback to summary
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
registerSynchronization
registerSynchronizationback to summary
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 org.springframework.core.Ordered interface. They will be executed in an order according to their order value (if any).

Parameters
synchronization:TransactionSynchronization

the synchronization object to register

Exceptions
IllegalStateException:
if transaction synchronization is not active
See Also
org.springframework.core.Ordered
setActualTransactionActiveback to summary
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:boolean

true to mark the current context as being associated with an actual transaction; false to reset that marker

setCurrentTransactionIsolationLevelback to summary
public void setCurrentTransactionIsolationLevel(Integer isolationLevel)

Expose an isolation level for the current transaction. Called by the transaction manager on transaction begin and on cleanup.

Parameters
isolationLevel:Integer

the isolation level to expose, according to the R2DBC Connection constants (equivalent to the corresponding Spring TransactionDefinition constants), or null to reset it

See Also
org.springframework.transaction.TransactionDefinition#ISOLATION_READ_UNCOMMITTED, org.springframework.transaction.TransactionDefinition#ISOLATION_READ_COMMITTED, org.springframework.transaction.TransactionDefinition#ISOLATION_REPEATABLE_READ, org.springframework.transaction.TransactionDefinition#ISOLATION_SERIALIZABLE, org.springframework.transaction.TransactionDefinition#getIsolationLevel()
setCurrentTransactionNameback to summary
public void setCurrentTransactionName(String name)

Expose the name of the current transaction, if any. Called by the transaction manager on transaction begin and on cleanup.

Parameters
name:String

the name of the transaction, or null to reset it

See Also
org.springframework.transaction.TransactionDefinition#getName()
setCurrentTransactionReadOnlyback to summary
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:boolean

true to mark the current transaction as read-only; false to reset such a read-only marker

See Also
org.springframework.transaction.TransactionDefinition#isReadOnly()
unbindResourceback to summary
public Object unbindResource(Object key) throws IllegalStateException

Unbind a resource for the given key from the current context.

Parameters
key:Object

the key to unbind (usually the resource factory)

Returns:Object

the previously bound value (usually the active resource object)

Exceptions
IllegalStateException:
if there is no value bound to the context
unbindResourceIfPossibleback to summary
public Object unbindResourceIfPossible(Object key)

Unbind a resource for the given key from the current context.

Parameters
key:Object

the key to unbind (usually the resource factory)

Returns:Object

the previously bound value, or null if none bound

Annotations
@Nullable