Class MongoTransactionManager

java.lang.Object
org.springframework.transaction.support.AbstractPlatformTransactionManager
org.springframework.data.mongodb.MongoTransactionManager
All Implemented Interfaces:
Serializable, org.springframework.beans.factory.InitializingBean, org.springframework.transaction.PlatformTransactionManager, org.springframework.transaction.support.ResourceTransactionManager, org.springframework.transaction.TransactionManager

public class MongoTransactionManager extends org.springframework.transaction.support.AbstractPlatformTransactionManager implements org.springframework.transaction.support.ResourceTransactionManager, org.springframework.beans.factory.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 Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    protected static class 
    MongoDB specific transaction object, representing a MongoResourceHolder.

    Nested classes/interfaces inherited from class org.springframework.transaction.support.AbstractPlatformTransactionManager

    org.springframework.transaction.support.AbstractPlatformTransactionManager.SuspendedResourcesHolder
  • Field Summary

    Fields inherited from class org.springframework.transaction.support.AbstractPlatformTransactionManager

    logger, SYNCHRONIZATION_ALWAYS, SYNCHRONIZATION_NEVER, SYNCHRONIZATION_ON_ACTUAL_TRANSACTION
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a new MongoTransactionManager for bean-style usage.
    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.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    protected void
    doBegin(Object transaction, org.springframework.transaction.TransactionDefinition definition)
     
    protected void
     
    protected void
    Customization hook to perform an actual commit of the given transaction.
    If a commit operation encounters an error, the MongoDB driver throws a MongoException holding error labels.
    protected final void
    doCommit(org.springframework.transaction.support.DefaultTransactionStatus status)
     
    protected Object
     
    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)
     
    Get the MongoDatabaseFactory that this instance manages transactions for.
     
    protected boolean
     
    void
    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.

    Methods inherited from class org.springframework.transaction.support.AbstractPlatformTransactionManager

    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

    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.PlatformTransactionManager

    commit, getTransaction, rollback
  • Constructor Details

  • Method Details

    • doGetTransaction

      protected Object doGetTransaction() throws org.springframework.transaction.TransactionException
      Specified by:
      doGetTransaction in class org.springframework.transaction.support.AbstractPlatformTransactionManager
      Throws:
      org.springframework.transaction.TransactionException
    • isExistingTransaction

      protected boolean isExistingTransaction(Object transaction) throws org.springframework.transaction.TransactionException
      Overrides:
      isExistingTransaction in class org.springframework.transaction.support.AbstractPlatformTransactionManager
      Throws:
      org.springframework.transaction.TransactionException
    • doBegin

      protected void doBegin(Object transaction, org.springframework.transaction.TransactionDefinition definition) throws org.springframework.transaction.TransactionException
      Specified by:
      doBegin in class org.springframework.transaction.support.AbstractPlatformTransactionManager
      Throws:
      org.springframework.transaction.TransactionException
    • doSuspend

      protected Object doSuspend(Object transaction) throws org.springframework.transaction.TransactionException
      Overrides:
      doSuspend in class org.springframework.transaction.support.AbstractPlatformTransactionManager
      Throws:
      org.springframework.transaction.TransactionException
    • doResume

      protected void doResume(@Nullable Object transaction, Object suspendedResources)
      Overrides:
      doResume in class org.springframework.transaction.support.AbstractPlatformTransactionManager
    • doCommit

      protected final void doCommit(org.springframework.transaction.support.DefaultTransactionStatus status) throws org.springframework.transaction.TransactionException
      Specified by:
      doCommit in class org.springframework.transaction.support.AbstractPlatformTransactionManager
      Throws:
      org.springframework.transaction.TransactionException
    • doCommit

      protected 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 a MongoException holding error labels.
      By default those labels are ignored, nevertheless one might check for 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);
       
       
      Parameters:
      transactionObject - never null.
      Throws:
      Exception - in case of transaction errors.
    • doRollback

      protected void doRollback(org.springframework.transaction.support.DefaultTransactionStatus status) throws org.springframework.transaction.TransactionException
      Specified by:
      doRollback in class org.springframework.transaction.support.AbstractPlatformTransactionManager
      Throws:
      org.springframework.transaction.TransactionException
    • doSetRollbackOnly

      protected void doSetRollbackOnly(org.springframework.transaction.support.DefaultTransactionStatus status) throws org.springframework.transaction.TransactionException
      Overrides:
      doSetRollbackOnly in class org.springframework.transaction.support.AbstractPlatformTransactionManager
      Throws:
      org.springframework.transaction.TransactionException
    • doCleanupAfterCompletion

      protected void doCleanupAfterCompletion(Object transaction)
      Overrides:
      doCleanupAfterCompletion in class org.springframework.transaction.support.AbstractPlatformTransactionManager
    • setDbFactory

      public void setDbFactory(MongoDatabaseFactory dbFactory)
      Set the MongoDatabaseFactory that this instance should manage transactions for.
      Parameters:
      dbFactory - must not be null.
    • setOptions

      public void setOptions(@Nullable com.mongodb.TransactionOptions options)
      Set the TransactionOptions to be applied when starting transactions.
      Parameters:
      options - can be null.
    • getDbFactory

      @Nullable public MongoDatabaseFactory getDbFactory()
      Get the MongoDatabaseFactory that this instance manages transactions for.
      Returns:
      can be null.
    • getResourceFactory

      public MongoDatabaseFactory getResourceFactory()
      Specified by:
      getResourceFactory in interface org.springframework.transaction.support.ResourceTransactionManager
    • afterPropertiesSet

      public void afterPropertiesSet()
      Specified by:
      afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean