Package org.springframework.data.mongodb
Class ReactiveMongoTransactionManager
java.lang.Object
org.springframework.transaction.reactive.AbstractReactiveTransactionManager
org.springframework.data.mongodb.ReactiveMongoTransactionManager
- All Implemented Interfaces:
Serializable
,InitializingBean
,ConfigurableTransactionManager
,ReactiveTransactionManager
,TransactionManager
public class ReactiveMongoTransactionManager
extends AbstractReactiveTransactionManager
implements 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
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.ReactiveMongoTransactionManager
(ReactiveMongoDatabaseFactory databaseFactory, MongoTransactionOptionsResolver transactionOptionsResolver, MongoTransactionOptions defaultTransactionOptions) 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
(TransactionSynchronizationManager synchronizationManager, Object transaction, TransactionDefinition definition) protected reactor.core.publisher.Mono<Void>
doCleanupAfterCompletion
(TransactionSynchronizationManager synchronizationManager, Object transaction) protected reactor.core.publisher.Mono<Void>
doCommit
(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
(TransactionSynchronizationManager synchronizationManager, GenericReactiveTransaction status) protected Object
doGetTransaction
(TransactionSynchronizationManager synchronizationManager) protected reactor.core.publisher.Mono<Void>
doResume
(TransactionSynchronizationManager synchronizationManager, Object transaction, Object suspendedResources) protected reactor.core.publisher.Mono<Void>
doRollback
(TransactionSynchronizationManager synchronizationManager, GenericReactiveTransaction status) protected reactor.core.publisher.Mono<Void>
doSetRollbackOnly
(TransactionSynchronizationManager synchronizationManager, GenericReactiveTransaction status) protected reactor.core.publisher.Mono<Object>
doSuspend
(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, getTransactionExecutionListeners, prepareForCommit, registerAfterCompletionWithExistingTransaction, rollback, setTransactionExecutionListeners
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.transaction.ConfigurableTransactionManager
addListener
-
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.
-
ReactiveMongoTransactionManager
public ReactiveMongoTransactionManager(ReactiveMongoDatabaseFactory databaseFactory, MongoTransactionOptionsResolver transactionOptionsResolver, @Nullable MongoTransactionOptions defaultTransactionOptions) Create a newReactiveMongoTransactionManager
obtaining sessions from the givenReactiveMongoDatabaseFactory
applying the givenoptions
, if present, when starting a new transaction.- Parameters:
databaseFactory
- must not be null.transactionOptionsResolver
- must not be null.defaultTransactionOptions
- can be null.- Since:
- 4.3
-
-
Method Details
-
doGetTransaction
protected Object doGetTransaction(TransactionSynchronizationManager synchronizationManager) throws TransactionException - Specified by:
doGetTransaction
in classAbstractReactiveTransactionManager
- Throws:
TransactionException
-
isExistingTransaction
- Overrides:
isExistingTransaction
in classAbstractReactiveTransactionManager
- Throws:
TransactionException
-
doBegin
protected reactor.core.publisher.Mono<Void> doBegin(TransactionSynchronizationManager synchronizationManager, Object transaction, TransactionDefinition definition) throws TransactionException - Specified by:
doBegin
in classAbstractReactiveTransactionManager
- Throws:
TransactionException
-
doSuspend
protected reactor.core.publisher.Mono<Object> doSuspend(TransactionSynchronizationManager synchronizationManager, Object transaction) throws TransactionException - Overrides:
doSuspend
in classAbstractReactiveTransactionManager
- Throws:
TransactionException
-
doResume
protected reactor.core.publisher.Mono<Void> doResume(TransactionSynchronizationManager synchronizationManager, @Nullable Object transaction, Object suspendedResources) - Overrides:
doResume
in classAbstractReactiveTransactionManager
-
doCommit
protected final reactor.core.publisher.Mono<Void> doCommit(TransactionSynchronizationManager synchronizationManager, GenericReactiveTransaction status) throws TransactionException - Specified by:
doCommit
in classAbstractReactiveTransactionManager
- Throws:
TransactionException
-
doCommit
protected reactor.core.publisher.Mono<Void> doCommit(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(TransactionSynchronizationManager synchronizationManager, GenericReactiveTransaction status) - Specified by:
doRollback
in classAbstractReactiveTransactionManager
-
doSetRollbackOnly
protected reactor.core.publisher.Mono<Void> doSetRollbackOnly(TransactionSynchronizationManager synchronizationManager, GenericReactiveTransaction status) throws TransactionException - Overrides:
doSetRollbackOnly
in classAbstractReactiveTransactionManager
- Throws:
TransactionException
-
doCleanupAfterCompletion
protected reactor.core.publisher.Mono<Void> doCleanupAfterCompletion(TransactionSynchronizationManager synchronizationManager, Object transaction) - Overrides:
doCleanupAfterCompletion
in classAbstractReactiveTransactionManager
-
setDatabaseFactory
Set theReactiveMongoDatabaseFactory
that this instance should manage transactions for.- Parameters:
databaseFactory
- must not be null.
-
setOptions
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 interfaceInitializingBean
-