Class RabbitTransactionManager
- java.lang.Object
-
- org.springframework.transaction.support.AbstractPlatformTransactionManager
-
- org.springframework.amqp.rabbit.transaction.RabbitTransactionManager
-
- All Implemented Interfaces:
java.io.Serializable,InitializingBean,PlatformTransactionManager,ResourceTransactionManager,TransactionManager
public class RabbitTransactionManager extends AbstractPlatformTransactionManager implements ResourceTransactionManager, InitializingBean
PlatformTransactionManagerimplementation for a single RabbitConnectionFactory. 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 standardConnection.createChannel(boolean)call with subsequent Channel creation. Spring'sRabbitTemplatewill autodetect a thread-bound Channel and automatically participate in it.The use of
CachingConnectionFactoryas 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.
- Author:
- Dave Syer
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.springframework.transaction.support.AbstractPlatformTransactionManager
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 RabbitTransactionManager()Create a new RabbitTransactionManager for bean-style usage.RabbitTransactionManager(ConnectionFactory connectionFactory)Create a new RabbitTransactionManager, given a ConnectionFactory.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidafterPropertiesSet()Make sure the ConnectionFactory has been set.protected voiddoBegin(java.lang.Object transaction, TransactionDefinition definition)protected voiddoCleanupAfterCompletion(java.lang.Object transaction)protected voiddoCommit(DefaultTransactionStatus status)protected java.lang.ObjectdoGetTransaction()protected voiddoResume(java.lang.Object transaction, java.lang.Object suspendedResources)protected voiddoRollback(DefaultTransactionStatus status)protected voiddoSetRollbackOnly(DefaultTransactionStatus status)protected java.lang.ObjectdoSuspend(java.lang.Object transaction)ConnectionFactorygetConnectionFactory()java.lang.ObjectgetResourceFactory()protected booleanisExistingTransaction(java.lang.Object transaction)voidsetConnectionFactory(ConnectionFactory connectionFactory)-
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 Detail
-
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.
-
RabbitTransactionManager
public RabbitTransactionManager(ConnectionFactory connectionFactory)
Create a new RabbitTransactionManager, given a ConnectionFactory.- Parameters:
connectionFactory- the ConnectionFactory to use
-
-
Method Detail
-
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:
afterPropertiesSetin interfaceInitializingBean
-
getResourceFactory
public java.lang.Object getResourceFactory()
- Specified by:
getResourceFactoryin interfaceResourceTransactionManager
-
doGetTransaction
protected java.lang.Object doGetTransaction()
- Specified by:
doGetTransactionin classAbstractPlatformTransactionManager
-
isExistingTransaction
protected boolean isExistingTransaction(java.lang.Object transaction)
- Overrides:
isExistingTransactionin classAbstractPlatformTransactionManager
-
doBegin
protected void doBegin(java.lang.Object transaction, TransactionDefinition definition)- Specified by:
doBeginin classAbstractPlatformTransactionManager
-
doSuspend
protected java.lang.Object doSuspend(java.lang.Object transaction)
- Overrides:
doSuspendin classAbstractPlatformTransactionManager
-
doResume
protected void doResume(java.lang.Object transaction, java.lang.Object suspendedResources)- Overrides:
doResumein classAbstractPlatformTransactionManager
-
doCommit
protected void doCommit(DefaultTransactionStatus status)
- Specified by:
doCommitin classAbstractPlatformTransactionManager
-
doRollback
protected void doRollback(DefaultTransactionStatus status)
- Specified by:
doRollbackin classAbstractPlatformTransactionManager
-
doSetRollbackOnly
protected void doSetRollbackOnly(DefaultTransactionStatus status)
- Overrides:
doSetRollbackOnlyin classAbstractPlatformTransactionManager
-
doCleanupAfterCompletion
protected void doCleanupAfterCompletion(java.lang.Object transaction)
- Overrides:
doCleanupAfterCompletionin classAbstractPlatformTransactionManager
-
-