public class MongoTransactionManager extends AbstractPlatformTransactionManager implements ResourceTransactionManager, InitializingBean
PlatformTransactionManager
implementation that manages
ClientSession
based transactions for a single MongoDbFactory
.
Binds a ClientSession
from the specified MongoDbFactory
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(MongoDbFactory)
instead of a standard MongoDbFactory.getDb()
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(MongoDbFactory, SessionSynchronization)
,
Serialized FormModifier and Type | Class and Description |
---|---|
protected static class |
MongoTransactionManager.MongoTransactionObject
MongoDB specific transaction object, representing a
MongoResourceHolder . |
AbstractPlatformTransactionManager.SuspendedResourcesHolder
logger, SYNCHRONIZATION_ALWAYS, SYNCHRONIZATION_NEVER, SYNCHRONIZATION_ON_ACTUAL_TRANSACTION
Constructor and Description |
---|
MongoTransactionManager()
Create a new
MongoTransactionManager for bean-style usage. |
MongoTransactionManager(MongoDbFactory dbFactory)
Create a new
MongoTransactionManager obtaining sessions from the given MongoDbFactory . |
MongoTransactionManager(MongoDbFactory dbFactory,
com.mongodb.TransactionOptions options)
Create a new
MongoTransactionManager obtaining sessions from the given MongoDbFactory applying the
given options , if present, when starting a new transaction. |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet() |
protected void |
doBegin(Object transaction,
TransactionDefinition definition) |
protected void |
doCleanupAfterCompletion(Object transaction) |
protected void |
doCommit(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(DefaultTransactionStatus status) |
protected void |
doSetRollbackOnly(DefaultTransactionStatus status) |
protected Object |
doSuspend(Object transaction) |
MongoDbFactory |
getDbFactory()
Get the
MongoDbFactory that this instance manages transactions for. |
MongoDbFactory |
getResourceFactory() |
protected boolean |
isExistingTransaction(Object transaction) |
void |
setDbFactory(MongoDbFactory dbFactory)
Set the
MongoDbFactory 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
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
commit, getTransaction, rollback
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
.public MongoTransactionManager(MongoDbFactory dbFactory)
MongoTransactionManager
obtaining sessions from the given MongoDbFactory
.dbFactory
- must not be null.public MongoTransactionManager(MongoDbFactory dbFactory, @Nullable com.mongodb.TransactionOptions options)
MongoTransactionManager
obtaining sessions from the given MongoDbFactory
applying the
given options
, if present, when starting a new transaction.dbFactory
- must not be null.options
- can be null.protected Object doGetTransaction() throws TransactionException
doGetTransaction
in class AbstractPlatformTransactionManager
TransactionException
protected boolean isExistingTransaction(Object transaction) throws TransactionException
isExistingTransaction
in class AbstractPlatformTransactionManager
TransactionException
protected void doBegin(Object transaction, TransactionDefinition definition) throws TransactionException
doBegin
in class AbstractPlatformTransactionManager
TransactionException
protected Object doSuspend(Object transaction) throws TransactionException
doSuspend
in class AbstractPlatformTransactionManager
TransactionException
protected void doResume(@Nullable Object transaction, Object suspendedResources)
doResume
in class AbstractPlatformTransactionManager
protected final void doCommit(DefaultTransactionStatus status) throws TransactionException
doCommit
in class AbstractPlatformTransactionManager
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(DefaultTransactionStatus status) throws TransactionException
doRollback
in class AbstractPlatformTransactionManager
TransactionException
protected void doSetRollbackOnly(DefaultTransactionStatus status) throws TransactionException
doSetRollbackOnly
in class AbstractPlatformTransactionManager
TransactionException
protected void doCleanupAfterCompletion(Object transaction)
doCleanupAfterCompletion
in class AbstractPlatformTransactionManager
public void setDbFactory(MongoDbFactory dbFactory)
MongoDbFactory
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 MongoDbFactory getDbFactory()
MongoDbFactory
that this instance manages transactions for.public MongoDbFactory getResourceFactory()
getResourceFactory
in interface ResourceTransactionManager
public void afterPropertiesSet()
afterPropertiesSet
in interface InitializingBean
Copyright © 2011–2020 Pivotal Software, Inc.. All rights reserved.