Class RabbitTransactionManager

java.lang.Object
org.springframework.transaction.support.AbstractPlatformTransactionManager
org.springframework.amqp.rabbit.transaction.RabbitTransactionManager
All Implemented Interfaces:
Serializable, org.springframework.beans.factory.InitializingBean, org.springframework.transaction.ConfigurableTransactionManager, org.springframework.transaction.PlatformTransactionManager, org.springframework.transaction.support.ResourceTransactionManager, org.springframework.transaction.TransactionManager

public class RabbitTransactionManager extends org.springframework.transaction.support.AbstractPlatformTransactionManager implements org.springframework.transaction.support.ResourceTransactionManager, org.springframework.beans.factory.InitializingBean
PlatformTransactionManager implementation for a single Rabbit ConnectionFactory. Binds a Rabbit Channel from the specified ConnectionFactory to the thread, potentially allowing for one thread-bound channel per ConnectionFactory.

This local strategy is an alternative to executing Rabbit operations within, and synchronized with, external transactions. This strategy is not able to provide XA transactions, for example in order to share transactions between messaging and database access.

Application code is required to retrieve the transactional Rabbit resources via ConnectionFactoryUtils.getTransactionalResourceHolder(ConnectionFactory, boolean) instead of a standard Connection.createChannel(boolean) call with subsequent Channel creation. Spring's RabbitTemplate will autodetect a thread-bound Channel and automatically participate in it.

The use of CachingConnectionFactory as a target for this transaction manager is strongly recommended. CachingConnectionFactory uses a single Rabbit Connection for all Rabbit access in order to avoid the overhead of repeated Connection creation, as well as maintaining a cache of Channels. Each transaction will then share the same Rabbit Connection, while still using its own individual Rabbit Channel.

Transaction synchronization is turned off by default, as this manager might be used alongside a datastore-based Spring transaction manager such as the JDBC org.springframework.jdbc.datasource.DataSourceTransactionManager, which has stronger needs for synchronization.

See Also:
  • Nested Class Summary

    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 RabbitTransactionManager for bean-style usage.
    Create a new RabbitTransactionManager, given a ConnectionFactory.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Make sure the ConnectionFactory has been set.
    protected void
    doBegin(Object transaction, org.springframework.transaction.TransactionDefinition definition)
     
    protected void
     
    protected 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)
     
     
     
    protected boolean
     
    void
     

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

    commit, determineTimeout, getDefaultTimeout, getTransaction, getTransactionExecutionListeners, getTransactionSynchronization, invokeAfterCompletion, isFailEarlyOnGlobalRollbackOnly, isGlobalRollbackOnParticipationFailure, isNestedTransactionAllowed, isRollbackOnCommitFailure, isValidateExistingTransaction, prepareForCommit, registerAfterCompletionWithExistingTransaction, resume, rollback, setDefaultTimeout, setFailEarlyOnGlobalRollbackOnly, setGlobalRollbackOnParticipationFailure, setNestedTransactionAllowed, setRollbackOnCommitFailure, setTransactionExecutionListeners, 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.ConfigurableTransactionManager

    addListener

    Methods inherited from interface org.springframework.transaction.PlatformTransactionManager

    commit, getTransaction, rollback
  • Constructor Details

    • RabbitTransactionManager

      public RabbitTransactionManager()
      Create a new RabbitTransactionManager for bean-style usage.

      Note: The ConnectionFactory has to be set before using the instance. This constructor can be used to prepare a RabbitTemplate via a BeanFactory, typically setting the ConnectionFactory via setConnectionFactory.

      Turns off transaction synchronization by default, as this manager might be used alongside a datastore-based Spring transaction manager like DataSourceTransactionManager, which has stronger needs for synchronization. Only one manager is allowed to drive synchronization at any point of time.

      See Also:
    • RabbitTransactionManager

      public RabbitTransactionManager(ConnectionFactory connectionFactory)
      Create a new RabbitTransactionManager, given a ConnectionFactory.
      Parameters:
      connectionFactory - the ConnectionFactory to use
  • Method Details

    • setConnectionFactory

      public void setConnectionFactory(ConnectionFactory connectionFactory)
      Parameters:
      connectionFactory - the connectionFactory to set
    • getConnectionFactory

      public ConnectionFactory getConnectionFactory()
      Returns:
      the connectionFactory
    • afterPropertiesSet

      public void afterPropertiesSet()
      Make sure the ConnectionFactory has been set.
      Specified by:
      afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
    • getResourceFactory

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

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

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

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

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

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

      protected void doCommit(org.springframework.transaction.support.DefaultTransactionStatus status)
      Specified by:
      doCommit in class org.springframework.transaction.support.AbstractPlatformTransactionManager
    • doRollback

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

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

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