public class MongoTransactionManager extends AbstractPlatformTransactionManager implements ResourceTransactionManager, InitializingBean
PlatformTransactionManager implementation that manages
ClientSession based transactions for a single MongoDatabaseFactory.
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.
MongoDatabase via
MongoDatabaseUtils.getDatabase(MongoDatabaseFactory) instead of a standard
MongoDatabaseFactory.getMongoDatabase() call. Spring classes such as
MongoTemplate use this strategy implicitly.
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 Form| Modifier and Type | Class and Description |
|---|---|
protected static class |
MongoTransactionManager.MongoTransactionObject
MongoDB specific transaction object, representing a
MongoResourceHolder. |
AbstractPlatformTransactionManager.SuspendedResourcesHolderlogger, SYNCHRONIZATION_ALWAYS, SYNCHRONIZATION_NEVER, SYNCHRONIZATION_ON_ACTUAL_TRANSACTION| 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,
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) |
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, useSavepointForNestedTransactionclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitcommit, getTransaction, rollbackpublic MongoTransactionManager()
MongoTransactionManager for bean-style usage.
db factory has to be
set before using the instance. Use this constructor to prepare a
MongoTransactionManager via a BeanFactory.
transaction options defining
ReadConcern and WriteConcern.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 TransactionException
doGetTransaction in class AbstractPlatformTransactionManagerTransactionExceptionprotected boolean isExistingTransaction(Object transaction) throws TransactionException
isExistingTransaction in class AbstractPlatformTransactionManagerTransactionExceptionprotected void doBegin(Object transaction, TransactionDefinition definition) throws TransactionException
doBegin in class AbstractPlatformTransactionManagerTransactionExceptionprotected Object doSuspend(Object transaction) throws TransactionException
doSuspend in class AbstractPlatformTransactionManagerTransactionExceptionprotected void doResume(@Nullable Object transaction, Object suspendedResources)
doResume in class AbstractPlatformTransactionManagerprotected final void doCommit(DefaultTransactionStatus status) throws TransactionException
doCommit in class AbstractPlatformTransactionManagerTransactionExceptionprotected 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 AbstractPlatformTransactionManagerTransactionExceptionprotected void doSetRollbackOnly(DefaultTransactionStatus status) throws TransactionException
doSetRollbackOnly in class AbstractPlatformTransactionManagerTransactionExceptionprotected void doCleanupAfterCompletion(Object transaction)
doCleanupAfterCompletion in class AbstractPlatformTransactionManagerpublic 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 ResourceTransactionManagerpublic void afterPropertiesSet()
afterPropertiesSet in interface InitializingBeanCopyright © 2011–2022 Pivotal Software, Inc.. All rights reserved.