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

public abstract Class ReactiveResourceSynchronization<O, K>

extends Object
implements TransactionSynchronization
Class Inheritance
All Implemented Interfaces
org.springframework.transaction.reactive.TransactionSynchronization
Type Parameters
<O>
the resource holder type
<K>
the resource key type
Imports
reactor.core.publisher.Mono

TransactionSynchronization implementation that manages a resource object bound through TransactionSynchronizationManager.
Authors
Mark Paluch, Juergen Hoeller
Since
5.2

Field Summary

Modifier and TypeField and Description
private volatile boolean
private final K
private final O
private final TransactionSynchronizationManager

Constructor Summary

AccessConstructor and Description
public
ReactiveResourceSynchronization(O
the resource object to manage
resourceObject
,
K
the key to bind the resource object for
resourceKey
,
TransactionSynchronizationManager
the synchronization manager bound to the current transaction
synchronizationManager
)

Create a new ReactiveResourceSynchronization for the given holder.

Method Summary

Modifier and TypeMethod and Description
public Mono<Void>
public Mono<Void>
afterCompletion(int
completion status according to the STATUS_* constants
status
)

Overrides default org.springframework.transaction.reactive.TransactionSynchronization.afterCompletion.

Invoked after transaction commit/rollback.
public Mono<Void>
beforeCommit(boolean
whether the transaction is defined as read-only transaction
readOnly
)

Overrides default org.springframework.transaction.reactive.TransactionSynchronization.beforeCommit.

Invoked before transaction commit (before "beforeCompletion").
public Mono<Void>
protected Mono<Void>
cleanupResource(O
the resource holder to process
resourceHolder
,
K
the key that the resource object was bound for
resourceKey
,
boolean
whether the transaction has committed (true) or rolled back (false)
committed
)

Perform a cleanup on the given resource (which is left bound to the thread).

protected Mono<Void>
processResourceAfterCommit(O
the resource holder to process
resourceHolder
)

After-commit callback for the given resource holder.

protected Mono<Void>
releaseResource(O
the resource holder to process
resourceHolder
,
K
the key that the resource object was bound for
resourceKey
)

Release the given resource (after it has been unbound from the thread).

public Mono<Void>
protected boolean
shouldReleaseAfterCompletion(O resourceHolder)

Return whether this holder's resource should be released after transaction completion (true).

protected boolean
shouldReleaseBeforeCompletion()

Return whether this holder's resource should be released before transaction completion (true) or rather after transaction completion (false).

protected boolean
shouldUnbindAtCompletion()

Return whether this holder should be unbound at completion (or should rather be left bound to the thread after the transaction).

public Mono<Void>
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

holderActiveback to summary
private volatile boolean holderActive
resourceKeyback to summary
private final K resourceKey
resourceObjectback to summary
private final O resourceObject
synchronizationManagerback to summary
private final TransactionSynchronizationManager synchronizationManager

Constructor Detail

ReactiveResourceSynchronizationback to summary
public ReactiveResourceSynchronization(O resourceObject, K resourceKey, TransactionSynchronizationManager synchronizationManager)

Create a new ReactiveResourceSynchronization for the given holder.

Parameters
resourceObject:O

the resource object to manage

resourceKey:K

the key to bind the resource object for

synchronizationManager:TransactionSynchronizationManager

the synchronization manager bound to the current transaction

See Also
TransactionSynchronizationManager#bindResource

Method Detail

afterCommitback to summary
public Mono<Void> afterCommit()

Overrides default org.springframework.transaction.reactive.TransactionSynchronization.afterCommit.

Doc from org.springframework.transaction.reactive.TransactionSynchronization.afterCommit.

Invoked after transaction commit. Can perform further operations right after the main transaction has successfully committed.

Can e.g. commit further operations that are supposed to follow on a successful commit of the main transaction, like confirmation messages or emails.

Note

The transaction will have been committed already, but the transactional resources might still be active and accessible. As a consequence, any data access code triggered at this point will still "participate" in the original transaction, allowing to perform some cleanup (with no commit following anymore!), unless it explicitly declares that it needs to run in a separate transaction. Hence: Use PROPAGATION_REQUIRES_NEW for any transactional operation that is called from here.

Annotations
@Override
afterCompletionback to summary
public Mono<Void> afterCompletion(int status)

Overrides default org.springframework.transaction.reactive.TransactionSynchronization.afterCompletion.

Doc from org.springframework.transaction.reactive.TransactionSynchronization.afterCompletion.

Invoked after transaction commit/rollback. Can perform resource cleanup after transaction completion.

Note

The transaction will have been committed or rolled back already, but the transactional resources might still be active and accessible. As a consequence, any data access code triggered at this point will still "participate" in the original transaction, allowing to perform some cleanup (with no commit following anymore!), unless it explicitly declares that it needs to run in a separate transaction. Hence: Use PROPAGATION_REQUIRES_NEW for any transactional operation that is called from here.

Parameters
status:int

completion status according to the STATUS_* constants

Annotations
@Override
beforeCommitback to summary
public Mono<Void> beforeCommit(boolean readOnly)

Overrides default org.springframework.transaction.reactive.TransactionSynchronization.beforeCommit.

Doc from org.springframework.transaction.reactive.TransactionSynchronization.beforeCommit.

Invoked before transaction commit (before "beforeCompletion").

This callback does not mean that the transaction will actually be committed. A rollback decision can still occur after this method has been called. This callback is rather meant to perform work that's only relevant if a commit still has a chance to happen, such as flushing SQL statements to the database.

Note that exceptions will get propagated to the commit caller and cause a rollback of the transaction.

Parameters
readOnly:boolean

whether the transaction is defined as read-only transaction

Annotations
@Override
beforeCompletionback to summary
public Mono<Void> beforeCompletion()

Overrides default org.springframework.transaction.reactive.TransactionSynchronization.beforeCompletion.

Doc from org.springframework.transaction.reactive.TransactionSynchronization.beforeCompletion.

Invoked before transaction commit/rollback. Can perform resource cleanup before transaction completion.

This method will be invoked after beforeCommit, even when beforeCommit threw an exception. This callback allows for closing resources before transaction completion, for any outcome.

Annotations
@Override
cleanupResourceback to summary
protected Mono<Void> cleanupResource(O resourceHolder, K resourceKey, boolean committed)

Perform a cleanup on the given resource (which is left bound to the thread).

Parameters
resourceHolder:O

the resource holder to process

resourceKey:K

the key that the resource object was bound for

committed:boolean

whether the transaction has committed (true) or rolled back (false)

processResourceAfterCommitback to summary
protected Mono<Void> processResourceAfterCommit(O resourceHolder)

After-commit callback for the given resource holder. Only called when the resource hasn't been released yet (shouldReleaseBeforeCompletion()).

Parameters
resourceHolder:O

the resource holder to process

releaseResourceback to summary
protected Mono<Void> releaseResource(O resourceHolder, K resourceKey)

Release the given resource (after it has been unbound from the thread).

Parameters
resourceHolder:O

the resource holder to process

resourceKey:K

the key that the resource object was bound for

resumeback to summary
public Mono<Void> resume()

Overrides default org.springframework.transaction.reactive.TransactionSynchronization.resume.

Doc from org.springframework.transaction.reactive.TransactionSynchronization.resume.

Resume this synchronization. Supposed to rebind resources to TransactionSynchronizationManager if managing any.

Annotations
@Override
shouldReleaseAfterCompletionback to summary
protected boolean shouldReleaseAfterCompletion(O resourceHolder)

Return whether this holder's resource should be released after transaction completion (true).

The default implementation returns !shouldReleaseBeforeCompletion(), releasing after completion if no attempt was made before completion.

See Also
releaseResource
shouldReleaseBeforeCompletionback to summary
protected boolean shouldReleaseBeforeCompletion()

Return whether this holder's resource should be released before transaction completion (true) or rather after transaction completion (false).

Note that resources will only be released when they are unbound from the thread (shouldUnbindAtCompletion()).

The default implementation returns true.

See Also
releaseResource
shouldUnbindAtCompletionback to summary
protected boolean shouldUnbindAtCompletion()

Return whether this holder should be unbound at completion (or should rather be left bound to the thread after the transaction).

The default implementation returns true.

suspendback to summary
public Mono<Void> suspend()

Overrides default org.springframework.transaction.reactive.TransactionSynchronization.suspend.

Doc from org.springframework.transaction.reactive.TransactionSynchronization.suspend.

Suspend this synchronization. Supposed to unbind resources from TransactionSynchronizationManager if managing any.

Annotations
@Override