spring-jms

org.springframework.jms.connection
Class JmsTransactionManager

java.lang.Object
  extended by org.springframework.transaction.support.AbstractPlatformTransactionManager
      extended by org.springframework.jms.connection.JmsTransactionManager
All Implemented Interfaces:
java.io.Serializable, org.springframework.beans.factory.InitializingBean, org.springframework.transaction.PlatformTransactionManager, org.springframework.transaction.support.ResourceTransactionManager
Direct Known Subclasses:
JmsTransactionManager102

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

Since:
1.1
Author:
Juergen Hoeller
See Also:
ConnectionFactoryUtils.getTransactionalSession(javax.jms.ConnectionFactory, javax.jms.Connection, boolean), TransactionAwareConnectionFactoryProxy, JmsTemplate, Serialized Form

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
JmsTransactionManager()
          Create a new JmsTransactionManager for bean-style usage.
JmsTransactionManager(javax.jms.ConnectionFactory connectionFactory)
          Create a new JmsTransactionManager, given a ConnectionFactory.
 
Method Summary
 void afterPropertiesSet()
          Make sure the ConnectionFactory has been set.
protected  javax.jms.Connection createConnection()
          Create a JMS Connection via this template's ConnectionFactory.
protected  javax.jms.Session createSession(javax.jms.Connection con)
          Create a JMS Session for the given Connection.
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)
           
 javax.jms.ConnectionFactory getConnectionFactory()
          Return the JMS ConnectionFactory that this instance should manage transactions for.
 java.lang.Object getResourceFactory()
           
protected  boolean isExistingTransaction(java.lang.Object transaction)
           
 void setConnectionFactory(javax.jms.ConnectionFactory cf)
          Set the JMS ConnectionFactory that this instance should manage transactions for.
 
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

JmsTransactionManager

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

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.

See Also:
setConnectionFactory(javax.jms.ConnectionFactory), AbstractPlatformTransactionManager.setTransactionSynchronization(int)

JmsTransactionManager

public JmsTransactionManager(javax.jms.ConnectionFactory connectionFactory)
Create a new JmsTransactionManager, given a ConnectionFactory.

Parameters:
connectionFactory - the ConnectionFactory to obtain connections from
Method Detail

setConnectionFactory

public void setConnectionFactory(javax.jms.ConnectionFactory cf)
Set the JMS ConnectionFactory that this instance should manage transactions for.


getConnectionFactory

public javax.jms.ConnectionFactory getConnectionFactory()
Return the JMS ConnectionFactory that this instance should manage transactions for.


afterPropertiesSet

public void afterPropertiesSet()
Make sure the ConnectionFactory has been set.

Specified by:
afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean

getResourceFactory

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

doGetTransaction

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

isExistingTransaction

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

doBegin

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

doSuspend

protected java.lang.Object doSuspend(java.lang.Object transaction)
Overrides:
doSuspend in class org.springframework.transaction.support.AbstractPlatformTransactionManager

doResume

protected void doResume(java.lang.Object transaction,
                        java.lang.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(java.lang.Object transaction)
Overrides:
doCleanupAfterCompletion in class org.springframework.transaction.support.AbstractPlatformTransactionManager

createConnection

protected javax.jms.Connection createConnection()
                                         throws javax.jms.JMSException
Create a JMS Connection via this template's ConnectionFactory.

This implementation uses JMS 1.1 API.

Returns:
the new JMS Connection
Throws:
javax.jms.JMSException - if thrown by JMS API methods

createSession

protected javax.jms.Session createSession(javax.jms.Connection con)
                                   throws javax.jms.JMSException
Create a JMS Session for the given Connection.

This implementation uses JMS 1.1 API.

Parameters:
con - the JMS Connection to create a Session for
Returns:
the new JMS Session
Throws:
javax.jms.JMSException - if thrown by JMS API methods

spring-jms