public class JmsTransactionManager
extends org.springframework.transaction.support.AbstractPlatformTransactionManager
implements org.springframework.transaction.support.ResourceTransactionManager, org.springframework.beans.factory.InitializingBean
PlatformTransactionManager
implementation
for a single JMS ConnectionFactory
. Binds a JMS
Connection/Session pair from the specified ConnectionFactory to the thread,
potentially allowing for one thread-bound Session per ConnectionFactory.
This local strategy is an alternative to executing JMS operations within
JTA transactions. Its advantage is that it is able to work in any environment,
for example a standalone application or a test suite, with any message broker
as target. However, this strategy is not able to provide XA transactions,
for example in order to share transactions between messaging and database access.
A full JTA/XA setup is required for XA transactions, typically using Spring's
org.springframework.transaction.jta.JtaTransactionManager
as strategy.
Application code is required to retrieve the transactional JMS Session via
ConnectionFactoryUtils.getTransactionalSession(javax.jms.ConnectionFactory, javax.jms.Connection, boolean)
instead of a standard
Java EE-style ConnectionFactory.createConnection()
call with subsequent
Session creation. Spring's JmsTemplate
will autodetect a thread-bound Session and automatically participate in it.
Alternatively, you can allow application code to work with the standard
Java EE-style lookup pattern on a ConnectionFactory, for example for legacy code
that is not aware of Spring at all. In that case, define a
TransactionAwareConnectionFactoryProxy
for your target ConnectionFactory,
which will automatically participate in Spring-managed transactions.
The use of CachingConnectionFactory
as a target for this
transaction manager is strongly recommended. CachingConnectionFactory
uses a single JMS Connection for all JMS access in order to avoid the overhead
of repeated Connection creation, as well as maintaining a cache of Sessions.
Each transaction will then share the same JMS Connection, while still using
its own individual JMS Session.
The use of a raw target ConnectionFactory would not only be inefficient
because of the lack of resource reuse. It might also lead to strange effects
when your JMS driver doesn't accept MessageProducer.close()
calls
and/or MessageConsumer.close()
calls before Session.commit()
,
with the latter supposed to commit all the messages that have been sent through the
producer handle and received through the consumer handle. As a safe general solution,
always pass in a CachingConnectionFactory
into this transaction manager's
"connectionFactory"
property.
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.
ConnectionFactoryUtils.getTransactionalSession(javax.jms.ConnectionFactory, javax.jms.Connection, boolean)
,
TransactionAwareConnectionFactoryProxy
,
JmsTemplate
,
Serialized FormConstructor and Description |
---|
JmsTransactionManager()
Create a new JmsTransactionManager for bean-style usage.
|
JmsTransactionManager(ConnectionFactory connectionFactory)
Create a new JmsTransactionManager, given a ConnectionFactory.
|
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet()
Make sure the ConnectionFactory has been set.
|
protected Connection |
createConnection()
Create a JMS Connection via this template's ConnectionFactory.
|
protected Session |
createSession(Connection con)
Create a JMS Session for the given Connection.
|
protected void |
doBegin(Object transaction,
org.springframework.transaction.TransactionDefinition definition) |
protected void |
doCleanupAfterCompletion(Object transaction) |
protected void |
doCommit(org.springframework.transaction.support.DefaultTransactionStatus status) |
protected Object |
doGetTransaction() |
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) |
ConnectionFactory |
getConnectionFactory()
Return the JMS ConnectionFactory that this instance should manage transactions for.
|
Object |
getResourceFactory() |
protected boolean |
isExistingTransaction(Object transaction) |
protected ConnectionFactory |
obtainConnectionFactory()
Obtain the ConnectionFactory for actual use.
|
void |
setConnectionFactory(ConnectionFactory cf)
Set the JMS ConnectionFactory that this instance should manage transactions for.
|
void |
setLazyResourceRetrieval(boolean lazyResourceRetrieval)
Specify whether this transaction manager should lazily retrieve a JMS
Connection and Session on access within a transaction (
true ). |
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 JmsTransactionManager()
Note: The ConnectionFactory has to be set before using the instance. This constructor can be used to prepare a JmsTemplate 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(javax.jms.ConnectionFactory)
,
AbstractPlatformTransactionManager.setTransactionSynchronization(int)
public JmsTransactionManager(ConnectionFactory connectionFactory)
connectionFactory
- the ConnectionFactory to obtain connections frompublic void setConnectionFactory(@Nullable ConnectionFactory cf)
@Nullable public ConnectionFactory getConnectionFactory()
protected final ConnectionFactory obtainConnectionFactory()
null
)IllegalStateException
- in case of no ConnectionFactory setpublic void setLazyResourceRetrieval(boolean lazyResourceRetrieval)
true
).
By default, it will eagerly create a JMS Connection and Session at
transaction begin (false
).JmsResourceHolder.getConnection()
,
JmsResourceHolder.getSession()
public void afterPropertiesSet()
afterPropertiesSet
in interface org.springframework.beans.factory.InitializingBean
public Object getResourceFactory()
getResourceFactory
in interface org.springframework.transaction.support.ResourceTransactionManager
protected Object doGetTransaction()
doGetTransaction
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
protected boolean isExistingTransaction(Object transaction)
isExistingTransaction
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
protected void doBegin(Object transaction, org.springframework.transaction.TransactionDefinition definition)
doBegin
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
protected Object doSuspend(Object transaction)
doSuspend
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
protected void doResume(@Nullable Object transaction, 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(Object transaction)
doCleanupAfterCompletion
in class org.springframework.transaction.support.AbstractPlatformTransactionManager
protected Connection createConnection() throws JMSException
This implementation uses JMS 1.1 API.
JMSException
- if thrown by JMS API methodsprotected Session createSession(Connection con) throws JMSException
This implementation uses JMS 1.1 API.
con
- the JMS Connection to create a Session forJMSException
- if thrown by JMS API methods