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()
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.
Constructor and Description |
---|
RabbitTransactionManager()
Create a new RabbitTransactionManager for bean-style usage.
|
RabbitTransactionManager(ConnectionFactory connectionFactory)
Create a new RabbitTransactionManager, given a ConnectionFactory.
|
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet()
Make sure the ConnectionFactory has been set.
|
protected void |
doBegin(java.lang.Object transaction,
org.springframework.transaction.TransactionDefinition definition) |
protected void |
doCleanupAfterCompletion(java.lang.Object transaction) |
protected void |
doCommit(org.springframework.transaction.support.DefaultTransactionStatus status) |
protected java.lang.Object |
doGetTransaction() |
protected void |
doResume(java.lang.Object transaction,
java.lang.Object suspendedResources) |
protected void |
doRollback(org.springframework.transaction.support.DefaultTransactionStatus status) |
protected void |
doSetRollbackOnly(org.springframework.transaction.support.DefaultTransactionStatus status) |
protected java.lang.Object |
doSuspend(java.lang.Object transaction) |
ConnectionFactory |
getConnectionFactory() |
java.lang.Object |
getResourceFactory() |
protected boolean |
isExistingTransaction(java.lang.Object transaction) |
void |
setConnectionFactory(ConnectionFactory connectionFactory) |
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
public RabbitTransactionManager()
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.
setConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory)
,
AbstractPlatformTransactionManager.setTransactionSynchronization(int)
public RabbitTransactionManager(ConnectionFactory connectionFactory)
connectionFactory
- the ConnectionFactory to usepublic void setConnectionFactory(ConnectionFactory connectionFactory)
connectionFactory
- the connectionFactory to setpublic ConnectionFactory getConnectionFactory()
public void afterPropertiesSet()
afterPropertiesSet
in interface org.springframework.beans.factory.InitializingBean
public java.lang.Object getResourceFactory()
getResourceFactory
in interface org.springframework.transaction.support.ResourceTransactionManager
protected java.lang.Object doGetTransaction()
doGetTransaction
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
protected boolean isExistingTransaction(java.lang.Object transaction)
isExistingTransaction
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
protected void doBegin(java.lang.Object transaction, org.springframework.transaction.TransactionDefinition definition)
doBegin
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
protected java.lang.Object doSuspend(java.lang.Object transaction)
doSuspend
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
protected void doResume(java.lang.Object transaction, java.lang.Object suspendedResources)
doResume
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
protected void doCommit(org.springframework.transaction.support.DefaultTransactionStatus status)
doCommit
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
protected void doRollback(org.springframework.transaction.support.DefaultTransactionStatus status)
doRollback
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
protected void doSetRollbackOnly(org.springframework.transaction.support.DefaultTransactionStatus status)
doSetRollbackOnly
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
protected void doCleanupAfterCompletion(java.lang.Object transaction)
doCleanupAfterCompletion
in class org.springframework.transaction.support.AbstractPlatformTransactionManager