Package org.springframework.data.mongodb
Class MongoTransactionManager
java.lang.Object
org.springframework.transaction.support.AbstractPlatformTransactionManager
org.springframework.data.mongodb.MongoTransactionManager
- All Implemented Interfaces:
- Serializable,- InitializingBean,- ConfigurableTransactionManager,- PlatformTransactionManager,- ResourceTransactionManager,- TransactionManager
public class MongoTransactionManager
extends AbstractPlatformTransactionManager
implements ResourceTransactionManager, InitializingBean
A 
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.
- Since:
- 2.1
- Author:
- Christoph Strobl, Mark Paluch
- See Also:
- 
Nested Class SummaryNested ClassesModifier and TypeClassDescriptionprotected static classMongoDB specific transaction object, representing aMongoResourceHolder.Nested classes/interfaces inherited from class org.springframework.transaction.support.AbstractPlatformTransactionManagerAbstractPlatformTransactionManager.SuspendedResourcesHolder
- 
Field SummaryFields inherited from class org.springframework.transaction.support.AbstractPlatformTransactionManagerlogger, SYNCHRONIZATION_ALWAYS, SYNCHRONIZATION_NEVER, SYNCHRONIZATION_ON_ACTUAL_TRANSACTION
- 
Constructor SummaryConstructorsConstructorDescriptionCreate a newMongoTransactionManagerfor bean-style usage.MongoTransactionManager(MongoDatabaseFactory databaseFactory) Create a newMongoTransactionManagerobtaining sessions from the givenMongoDatabaseFactory.MongoTransactionManager(MongoDatabaseFactory databaseFactory, com.mongodb.TransactionOptions options) Create a newMongoTransactionManagerobtaining sessions from the givenMongoDatabaseFactoryapplying the givenoptions, if present, when starting a new transaction.MongoTransactionManager(MongoDatabaseFactory databaseFactory, MongoTransactionOptionsResolver transactionOptionsResolver, MongoTransactionOptions defaultTransactionOptions) Create a newMongoTransactionManagerobtaining sessions from the givenMongoDatabaseFactoryapplying the givenoptions, if present, when starting a new transaction.
- 
Method SummaryModifier and TypeMethodDescriptionvoidprotected voiddoBegin(Object transaction, TransactionDefinition definition) protected voiddoCleanupAfterCompletion(Object transaction) protected voiddoCommit(MongoTransactionManager.MongoTransactionObject transactionObject) Customization hook to perform an actual commit of the given transaction.
 If a commit operation encounters an error, the MongoDB driver throws aMongoExceptionholding error labels.protected final voiddoCommit(DefaultTransactionStatus status) protected Objectprotected voidprotected voiddoRollback(DefaultTransactionStatus status) protected voidprotected ObjectGet theMongoDatabaseFactorythat this instance manages transactions for.protected booleanisExistingTransaction(Object transaction) voidsetDatabaseFactory(MongoDatabaseFactory databaseFactory) Set theMongoDatabaseFactorythat this instance should manage transactions for.voidsetOptions(com.mongodb.TransactionOptions options) Set theTransactionOptionsto be applied when starting transactions.Methods inherited from class org.springframework.transaction.support.AbstractPlatformTransactionManagercommit, determineTimeout, getDefaultTimeout, getTransaction, getTransactionExecutionListeners, getTransactionSynchronization, invokeAfterCompletion, isFailEarlyOnGlobalRollbackOnly, isGlobalRollbackOnParticipationFailure, isNestedTransactionAllowed, isRollbackOnCommitFailure, isValidateExistingTransaction, prepareForCommit, prepareSynchronization, registerAfterCompletionWithExistingTransaction, resume, rollback, setDefaultTimeout, setFailEarlyOnGlobalRollbackOnly, setGlobalRollbackOnParticipationFailure, setNestedTransactionAllowed, setRollbackOnCommitFailure, setTransactionExecutionListeners, setTransactionSynchronization, setTransactionSynchronizationName, setValidateExistingTransaction, shouldCommitOnGlobalRollbackOnly, suspend, triggerBeforeCommit, triggerBeforeCompletion, useSavepointForNestedTransactionMethods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.transaction.ConfigurableTransactionManageraddListenerMethods inherited from interface org.springframework.transaction.PlatformTransactionManagercommit, getTransaction, rollback
- 
Constructor Details- 
MongoTransactionManagerpublic MongoTransactionManager()Create a newMongoTransactionManagerfor bean-style usage.
 Note:Thedb factoryhas to besetbefore using the instance. Use this constructor to prepare aMongoTransactionManagervia aBeanFactory.
 Optionally it is possible to set defaulttransaction optionsdefiningReadConcernandWriteConcern.
- 
MongoTransactionManagerCreate a newMongoTransactionManagerobtaining sessions from the givenMongoDatabaseFactory.- Parameters:
- databaseFactory- must not be null.
 
- 
MongoTransactionManagerpublic MongoTransactionManager(MongoDatabaseFactory databaseFactory, @Nullable com.mongodb.TransactionOptions options) Create a newMongoTransactionManagerobtaining sessions from the givenMongoDatabaseFactoryapplying the givenoptions, if present, when starting a new transaction.- Parameters:
- databaseFactory- must not be null.
- options- can be null.
 
- 
MongoTransactionManagerpublic MongoTransactionManager(MongoDatabaseFactory databaseFactory, MongoTransactionOptionsResolver transactionOptionsResolver, MongoTransactionOptions defaultTransactionOptions) Create a newMongoTransactionManagerobtaining sessions from the givenMongoDatabaseFactoryapplying 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- Specified by:
- doGetTransactionin class- AbstractPlatformTransactionManager
- Throws:
- TransactionException
 
- 
isExistingTransaction- Overrides:
- isExistingTransactionin class- AbstractPlatformTransactionManager
- Throws:
- TransactionException
 
- 
doBeginprotected void doBegin(Object transaction, TransactionDefinition definition) throws TransactionException - Specified by:
- doBeginin class- AbstractPlatformTransactionManager
- Throws:
- TransactionException
 
- 
doSuspend- Overrides:
- doSuspendin class- AbstractPlatformTransactionManager
- Throws:
- TransactionException
 
- 
doResume- Overrides:
- doResumein class- AbstractPlatformTransactionManager
 
- 
doCommit- Specified by:
- doCommitin class- AbstractPlatformTransactionManager
- Throws:
- TransactionException
 
- 
doCommitprotected void doCommit(MongoTransactionManager.MongoTransactionObject transactionObject) throws Exception Customization hook to perform an actual commit of the given transaction.
 If a commit operation encounters an error, the MongoDB driver throws aMongoExceptionholding error labels.
 By default those labels are ignored, nevertheless one might check fortransient commit errors labelsand 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);- Parameters:
- transactionObject- never null.
- Throws:
- Exception- in case of transaction errors.
 
- 
doRollback- Specified by:
- doRollbackin class- AbstractPlatformTransactionManager
- Throws:
- TransactionException
 
- 
doSetRollbackOnly- Overrides:
- doSetRollbackOnlyin class- AbstractPlatformTransactionManager
- Throws:
- TransactionException
 
- 
doCleanupAfterCompletion- Overrides:
- doCleanupAfterCompletionin class- AbstractPlatformTransactionManager
 
- 
setDatabaseFactorySet theMongoDatabaseFactorythat this instance should manage transactions for.- Parameters:
- databaseFactory- must not be null.
 
- 
setOptionsSet theTransactionOptionsto be applied when starting transactions.- Parameters:
- options- can be null.
 
- 
getDatabaseFactoryGet theMongoDatabaseFactorythat this instance manages transactions for.- Returns:
- can be null.
 
- 
getResourceFactory- Specified by:
- getResourceFactoryin interface- ResourceTransactionManager
 
- 
afterPropertiesSetpublic void afterPropertiesSet()- Specified by:
- afterPropertiesSetin interface- InitializingBean
 
 
-