Package org.springframework.data.mongodb
Class ReactiveMongoTransactionManager
java.lang.Object
org.springframework.transaction.reactive.AbstractReactiveTransactionManager
org.springframework.data.mongodb.ReactiveMongoTransactionManager
- All Implemented Interfaces:
Serializable
,org.springframework.beans.factory.InitializingBean
,org.springframework.transaction.ReactiveTransactionManager
,org.springframework.transaction.TransactionManager
public class ReactiveMongoTransactionManager
extends org.springframework.transaction.reactive.AbstractReactiveTransactionManager
implements org.springframework.beans.factory.InitializingBean
A
Binds a
Application code is required to retrieve the
By default failure of a commit operation raises a
ReactiveTransactionManager
implementation that manages
ClientSession
based transactions for a single
ReactiveMongoDatabaseFactory
.
Binds a
ClientSession
from the specified
ReactiveMongoDatabaseFactory
to the subscriber
Context
.
Readonly
transactions operate on a
ClientSession
and enable causal consistency, and also start
,
commit
or
abort
a transaction.
Application code is required to retrieve the
MongoDatabase
via
ReactiveMongoDatabaseUtils.getDatabase(ReactiveMongoDatabaseFactory)
instead
of a standard ReactiveMongoDatabaseFactory.getMongoDatabase()
call. Spring
classes such as ReactiveMongoTemplate
use this strategy implicitly.
By default failure of a commit operation raises a
TransactionSystemException
. You can override
doCommit(TransactionSynchronizationManager, ReactiveMongoTransactionObject)
to implement the
Retry Commit Operation
behavior as outlined in the MongoDB reference manual.- Since:
- 2.2
- Author:
- Christoph Strobl, Mark Paluch
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionprotected static class
MongoDB specific transaction object, representing aMongoResourceHolder
.Nested classes/interfaces inherited from class org.springframework.transaction.reactive.AbstractReactiveTransactionManager
org.springframework.transaction.reactive.AbstractReactiveTransactionManager.SuspendedResourcesHolder
-
Field Summary
Fields inherited from class org.springframework.transaction.reactive.AbstractReactiveTransactionManager
logger
-
Constructor Summary
ConstructorDescriptionCreate a newReactiveMongoTransactionManager
for bean-style usage.ReactiveMongoTransactionManager
(ReactiveMongoDatabaseFactory databaseFactory) Create a newReactiveMongoTransactionManager
obtaining sessions from the givenReactiveMongoDatabaseFactory
.ReactiveMongoTransactionManager
(ReactiveMongoDatabaseFactory databaseFactory, com.mongodb.TransactionOptions options) Create a newReactiveMongoTransactionManager
obtaining sessions from the givenReactiveMongoDatabaseFactory
applying the givenoptions
, if present, when starting a new transaction. -
Method Summary
Modifier and TypeMethodDescriptionvoid
protected reactor.core.publisher.Mono<Void>
doBegin
(org.springframework.transaction.reactive.TransactionSynchronizationManager synchronizationManager, Object transaction, org.springframework.transaction.TransactionDefinition definition) protected reactor.core.publisher.Mono<Void>
doCleanupAfterCompletion
(org.springframework.transaction.reactive.TransactionSynchronizationManager synchronizationManager, Object transaction) protected reactor.core.publisher.Mono<Void>
doCommit
(org.springframework.transaction.reactive.TransactionSynchronizationManager synchronizationManager, ReactiveMongoTransactionManager.ReactiveMongoTransactionObject transactionObject) Customization hook to perform an actual commit of the given transaction.
If a commit operation encounters an error, the MongoDB driver throws aMongoException
holding error labels.protected final reactor.core.publisher.Mono<Void>
doCommit
(org.springframework.transaction.reactive.TransactionSynchronizationManager synchronizationManager, org.springframework.transaction.reactive.GenericReactiveTransaction status) protected Object
doGetTransaction
(org.springframework.transaction.reactive.TransactionSynchronizationManager synchronizationManager) protected reactor.core.publisher.Mono<Void>
doResume
(org.springframework.transaction.reactive.TransactionSynchronizationManager synchronizationManager, Object transaction, Object suspendedResources) protected reactor.core.publisher.Mono<Void>
doRollback
(org.springframework.transaction.reactive.TransactionSynchronizationManager synchronizationManager, org.springframework.transaction.reactive.GenericReactiveTransaction status) protected reactor.core.publisher.Mono<Void>
doSetRollbackOnly
(org.springframework.transaction.reactive.TransactionSynchronizationManager synchronizationManager, org.springframework.transaction.reactive.GenericReactiveTransaction status) protected reactor.core.publisher.Mono<Object>
doSuspend
(org.springframework.transaction.reactive.TransactionSynchronizationManager synchronizationManager, Object transaction) Get theReactiveMongoDatabaseFactory
that this instance manages transactions for.protected boolean
isExistingTransaction
(Object transaction) void
setDatabaseFactory
(ReactiveMongoDatabaseFactory databaseFactory) Set theReactiveMongoDatabaseFactory
that this instance should manage transactions for.void
setOptions
(com.mongodb.TransactionOptions options) Set theTransactionOptions
to be applied when starting transactions.Methods inherited from class org.springframework.transaction.reactive.AbstractReactiveTransactionManager
commit, getReactiveTransaction, prepareForCommit, registerAfterCompletionWithExistingTransaction, rollback
-
Constructor Details
-
ReactiveMongoTransactionManager
public ReactiveMongoTransactionManager()Create a newReactiveMongoTransactionManager
for bean-style usage.
Note:Thedb factory
has to besetDatabaseFactory(ReactiveMongoDatabaseFactory)
set} before using the instance. Use this constructor to prepare aReactiveMongoTransactionManager
via aBeanFactory
.
Optionally it is possible to set defaulttransaction options
definingReadConcern
andWriteConcern
. -
ReactiveMongoTransactionManager
Create a newReactiveMongoTransactionManager
obtaining sessions from the givenReactiveMongoDatabaseFactory
.- Parameters:
databaseFactory
- must not be null.
-
ReactiveMongoTransactionManager
public ReactiveMongoTransactionManager(ReactiveMongoDatabaseFactory databaseFactory, @Nullable com.mongodb.TransactionOptions options) Create a newReactiveMongoTransactionManager
obtaining sessions from the givenReactiveMongoDatabaseFactory
applying the givenoptions
, if present, when starting a new transaction.- Parameters:
databaseFactory
- must not be null.options
- can be null.
-
-
Method Details
-
doGetTransaction
protected Object doGetTransaction(org.springframework.transaction.reactive.TransactionSynchronizationManager synchronizationManager) throws org.springframework.transaction.TransactionException - Specified by:
doGetTransaction
in classorg.springframework.transaction.reactive.AbstractReactiveTransactionManager
- Throws:
org.springframework.transaction.TransactionException
-
isExistingTransaction
protected boolean isExistingTransaction(Object transaction) throws org.springframework.transaction.TransactionException - Overrides:
isExistingTransaction
in classorg.springframework.transaction.reactive.AbstractReactiveTransactionManager
- Throws:
org.springframework.transaction.TransactionException
-
doBegin
protected reactor.core.publisher.Mono<Void> doBegin(org.springframework.transaction.reactive.TransactionSynchronizationManager synchronizationManager, Object transaction, org.springframework.transaction.TransactionDefinition definition) throws org.springframework.transaction.TransactionException - Specified by:
doBegin
in classorg.springframework.transaction.reactive.AbstractReactiveTransactionManager
- Throws:
org.springframework.transaction.TransactionException
-
doSuspend
protected reactor.core.publisher.Mono<Object> doSuspend(org.springframework.transaction.reactive.TransactionSynchronizationManager synchronizationManager, Object transaction) throws org.springframework.transaction.TransactionException - Overrides:
doSuspend
in classorg.springframework.transaction.reactive.AbstractReactiveTransactionManager
- Throws:
org.springframework.transaction.TransactionException
-
doResume
protected reactor.core.publisher.Mono<Void> doResume(org.springframework.transaction.reactive.TransactionSynchronizationManager synchronizationManager, @Nullable Object transaction, Object suspendedResources) - Overrides:
doResume
in classorg.springframework.transaction.reactive.AbstractReactiveTransactionManager
-
doCommit
protected final reactor.core.publisher.Mono<Void> doCommit(org.springframework.transaction.reactive.TransactionSynchronizationManager synchronizationManager, org.springframework.transaction.reactive.GenericReactiveTransaction status) throws org.springframework.transaction.TransactionException - Specified by:
doCommit
in classorg.springframework.transaction.reactive.AbstractReactiveTransactionManager
- Throws:
org.springframework.transaction.TransactionException
-
doCommit
protected reactor.core.publisher.Mono<Void> doCommit(org.springframework.transaction.reactive.TransactionSynchronizationManager synchronizationManager, ReactiveMongoTransactionManager.ReactiveMongoTransactionObject transactionObject) Customization hook to perform an actual commit of the given transaction.
If a commit operation encounters an error, the MongoDB driver throws aMongoException
holding error labels.
By default those labels are ignored, nevertheless one might check fortransient commit errors labels
and retry the the commit.- Parameters:
synchronizationManager
- reactive synchronization manager.transactionObject
- never null.
-
doRollback
protected reactor.core.publisher.Mono<Void> doRollback(org.springframework.transaction.reactive.TransactionSynchronizationManager synchronizationManager, org.springframework.transaction.reactive.GenericReactiveTransaction status) - Specified by:
doRollback
in classorg.springframework.transaction.reactive.AbstractReactiveTransactionManager
-
doSetRollbackOnly
protected reactor.core.publisher.Mono<Void> doSetRollbackOnly(org.springframework.transaction.reactive.TransactionSynchronizationManager synchronizationManager, org.springframework.transaction.reactive.GenericReactiveTransaction status) throws org.springframework.transaction.TransactionException - Overrides:
doSetRollbackOnly
in classorg.springframework.transaction.reactive.AbstractReactiveTransactionManager
- Throws:
org.springframework.transaction.TransactionException
-
doCleanupAfterCompletion
protected reactor.core.publisher.Mono<Void> doCleanupAfterCompletion(org.springframework.transaction.reactive.TransactionSynchronizationManager synchronizationManager, Object transaction) - Overrides:
doCleanupAfterCompletion
in classorg.springframework.transaction.reactive.AbstractReactiveTransactionManager
-
setDatabaseFactory
Set theReactiveMongoDatabaseFactory
that this instance should manage transactions for.- Parameters:
databaseFactory
- must not be null.
-
setOptions
public void setOptions(@Nullable com.mongodb.TransactionOptions options) Set theTransactionOptions
to be applied when starting transactions.- Parameters:
options
- can be null.
-
getDatabaseFactory
Get theReactiveMongoDatabaseFactory
that this instance manages transactions for.- Returns:
- can be null.
-
afterPropertiesSet
public void afterPropertiesSet()- Specified by:
afterPropertiesSet
in interfaceorg.springframework.beans.factory.InitializingBean
-