public class MongoTransactionManager
extends org.springframework.transaction.support.AbstractPlatformTransactionManager
implements org.springframework.transaction.support.ResourceTransactionManager, org.springframework.beans.factory.InitializingBean
PlatformTransactionManager
implementation that manages
ClientSession
based transactions for a single MongoDatabaseFactory
.
Binds a ClientSession
from the specified MongoDatabaseFactory
to the thread.
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
MongoDatabaseUtils.getDatabase(MongoDatabaseFactory)
instead of a standard
MongoDatabaseFactory.getMongoDatabase()
call. Spring classes such as
MongoTemplate
use this strategy implicitly.
By default failure of a commit operation raises a TransactionSystemException
. One may override
doCommit(MongoTransactionObject)
to implement the
Retry Commit Operation
behavior as outlined in the MongoDB reference manual.MongoDatabaseUtils.getDatabase(MongoDatabaseFactory, SessionSynchronization)
,
Serialized FormModifier and Type | Class and Description |
---|---|
protected static class |
MongoTransactionManager.MongoTransactionObject
MongoDB specific transaction object, representing a
MongoResourceHolder . |
Constructor and Description |
---|
MongoTransactionManager()
Create a new
MongoTransactionManager for bean-style usage. |
MongoTransactionManager(MongoDatabaseFactory dbFactory)
Create a new
MongoTransactionManager obtaining sessions from the given MongoDatabaseFactory . |
MongoTransactionManager(MongoDatabaseFactory dbFactory,
com.mongodb.TransactionOptions options)
Create a new
MongoTransactionManager obtaining sessions from the given MongoDatabaseFactory
applying the given options , if present, when starting a new transaction. |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet() |
protected void |
doBegin(Object transaction,
org.springframework.transaction.TransactionDefinition definition) |
protected void |
doCleanupAfterCompletion(Object transaction) |
protected void |
doCommit(org.springframework.transaction.support.DefaultTransactionStatus status) |
protected void |
doCommit(MongoTransactionManager.MongoTransactionObject transactionObject)
Customization hook to perform an actual commit of the given transaction.
|
protected Object |
doGetTransaction() |
protected void |
doResume(Object transaction,
Object suspendedResources) |
protected void |
doRollback(org.springframework.transaction.support.DefaultTransactionStatus status) |
protected void |
doSetRollbackOnly(org.springframework.transaction.support.DefaultTransactionStatus status) |
protected Object |
doSuspend(Object transaction) |
MongoDatabaseFactory |
getDbFactory()
Get the
MongoDatabaseFactory that this instance manages transactions for. |
MongoDatabaseFactory |
getResourceFactory() |
protected boolean |
isExistingTransaction(Object transaction) |
void |
setDbFactory(MongoDatabaseFactory dbFactory)
Set the
MongoDatabaseFactory that this instance should manage transactions for. |
void |
setOptions(com.mongodb.TransactionOptions options)
Set the
TransactionOptions to be applied when starting transactions. |
commit, determineTimeout, getDefaultTimeout, getTransaction, getTransactionSynchronization, invokeAfterCompletion, isFailEarlyOnGlobalRollbackOnly, isGlobalRollbackOnParticipationFailure, isNestedTransactionAllowed, isRollbackOnCommitFailure, isValidateExistingTransaction, newTransactionStatus, prepareForCommit, prepareSynchronization, prepareTransactionStatus, registerAfterCompletionWithExistingTransaction, resume, rollback, setDefaultTimeout, setFailEarlyOnGlobalRollbackOnly, setGlobalRollbackOnParticipationFailure, setNestedTransactionAllowed, setRollbackOnCommitFailure, setTransactionSynchronization, setTransactionSynchronizationName, setValidateExistingTransaction, shouldCommitOnGlobalRollbackOnly, suspend, triggerBeforeCommit, triggerBeforeCompletion, useSavepointForNestedTransaction
public MongoTransactionManager()
MongoTransactionManager
for bean-style usage.
Note:The db factory
has to be
set
before using the instance. Use this constructor to prepare a
MongoTransactionManager
via a BeanFactory
.
Optionally it is possible to set default transaction options
defining
ReadConcern
and WriteConcern
.setDbFactory(MongoDatabaseFactory)
,
AbstractPlatformTransactionManager.setTransactionSynchronization(int)
public MongoTransactionManager(MongoDatabaseFactory dbFactory)
MongoTransactionManager
obtaining sessions from the given MongoDatabaseFactory
.dbFactory
- must not be null.public MongoTransactionManager(MongoDatabaseFactory dbFactory, @Nullable com.mongodb.TransactionOptions options)
MongoTransactionManager
obtaining sessions from the given MongoDatabaseFactory
applying the given options
, if present, when starting a new transaction.dbFactory
- must not be null.options
- can be null.protected Object doGetTransaction() throws org.springframework.transaction.TransactionException
doGetTransaction
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
org.springframework.transaction.TransactionException
protected boolean isExistingTransaction(Object transaction) throws org.springframework.transaction.TransactionException
isExistingTransaction
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
org.springframework.transaction.TransactionException
protected void doBegin(Object transaction, org.springframework.transaction.TransactionDefinition definition) throws org.springframework.transaction.TransactionException
doBegin
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
org.springframework.transaction.TransactionException
protected Object doSuspend(Object transaction) throws org.springframework.transaction.TransactionException
doSuspend
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
org.springframework.transaction.TransactionException
protected void doResume(@Nullable Object transaction, Object suspendedResources)
doResume
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
protected final void doCommit(org.springframework.transaction.support.DefaultTransactionStatus status) throws org.springframework.transaction.TransactionException
doCommit
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
org.springframework.transaction.TransactionException
protected void doCommit(MongoTransactionManager.MongoTransactionObject transactionObject) throws Exception
MongoException
holding
error labels. transient commit errors labels
and retry the the
commit.
int retries = 3;
do {
try {
transactionObject.commitTransaction();
break;
} catch (MongoException ex) {
if (!ex.hasErrorLabel(MongoException.UNKNOWN_TRANSACTION_COMMIT_RESULT_LABEL)) {
throw ex;
}
}
Thread.sleep(500);
} while (--retries > 0);
transactionObject
- never null.Exception
- in case of transaction errors.protected void doRollback(org.springframework.transaction.support.DefaultTransactionStatus status) throws org.springframework.transaction.TransactionException
doRollback
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
org.springframework.transaction.TransactionException
protected void doSetRollbackOnly(org.springframework.transaction.support.DefaultTransactionStatus status) throws org.springframework.transaction.TransactionException
doSetRollbackOnly
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
org.springframework.transaction.TransactionException
protected void doCleanupAfterCompletion(Object transaction)
doCleanupAfterCompletion
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
public void setDbFactory(MongoDatabaseFactory dbFactory)
MongoDatabaseFactory
that this instance should manage transactions for.dbFactory
- must not be null.public void setOptions(@Nullable com.mongodb.TransactionOptions options)
TransactionOptions
to be applied when starting transactions.options
- can be null.@Nullable public MongoDatabaseFactory getDbFactory()
MongoDatabaseFactory
that this instance manages transactions for.public MongoDatabaseFactory getResourceFactory()
getResourceFactory
in interface org.springframework.transaction.support.ResourceTransactionManager
public void afterPropertiesSet()
afterPropertiesSet
in interface org.springframework.beans.factory.InitializingBean
Copyright © 2011–2021 Pivotal Software, Inc.. All rights reserved.