org.springframework.jms.connection
public class: JmsTransactionManager [javadoc |
source]
java.lang.Object
org.springframework.transaction.support.AbstractPlatformTransactionManager
org.springframework.jms.connection.JmsTransactionManager
All Implemented Interfaces:
InitializingBean, ResourceTransactionManager, PlatformTransactionManager, Serializable
Direct Known Subclasses:
JmsTransactionManager102
org.springframework.transaction.PlatformTransactionManager implementation
for a single JMS
javax.jms.ConnectionFactory . Binds a JMS
Connection/Session pair from the specified ConnectionFactory to the thread,
potentially allowing for one thread-bound Session per ConnectionFactory.
NOTE: This class requires a JMS 1.1+ provider because it builds on
the domain-independent API. Use the JmsTransactionManager102 subclass
for a JMS 1.0.2 provider, e.g. when running on a J2EE 1.3 server.
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 instead of a standard
J2EE-style ConnectionFactory#createConnection() call with subsequent
Session creation. Spring's org.springframework.jms.core.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.
This transaction strategy will typically be used in combination with
SingleConnectionFactory , which uses a single JMS Connection for all
JMS access in order to avoid the overhead of repeated Connection creation,
typically in a standalone application. Each transaction will then share the
same JMS Connection, while still using its own individual JMS Session.
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: |
public JmsTransactionManager() {
setTransactionSynchronization(SYNCHRONIZATION_NEVER);
}
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. |
public JmsTransactionManager(ConnectionFactory connectionFactory) {
this();
setConnectionFactory(connectionFactory);
afterPropertiesSet();
}
Create a new JmsTransactionManager, given a ConnectionFactory. Parameters:
connectionFactory - the ConnectionFactory to obtain connections from
|
| Method from org.springframework.jms.connection.JmsTransactionManager Summary: |
|---|
|
afterPropertiesSet, createConnection, createSession, doBegin, doCleanupAfterCompletion, doCommit, doGetTransaction, doResume, doRollback, doSetRollbackOnly, doSuspend, getConnectionFactory, getResourceFactory, isExistingTransaction, setConnectionFactory |
| Methods from org.springframework.transaction.support.AbstractPlatformTransactionManager: |
|---|
|
commit, determineTimeout, doBegin, doCleanupAfterCompletion, doCommit, doGetTransaction, doResume, doRollback, doSetRollbackOnly, doSuspend, getDefaultTimeout, getTransaction, getTransactionSynchronization, invokeAfterCompletion, isExistingTransaction, isFailEarlyOnGlobalRollbackOnly, isGlobalRollbackOnParticipationFailure, isNestedTransactionAllowed, isRollbackOnCommitFailure, isValidateExistingTransaction, newTransactionStatus, prepareForCommit, registerAfterCompletionWithExistingTransaction, resume, rollback, setDefaultTimeout, setFailEarlyOnGlobalRollbackOnly, setGlobalRollbackOnParticipationFailure, setNestedTransactionAllowed, setRollbackOnCommitFailure, setTransactionSynchronization, setTransactionSynchronizationName, setValidateExistingTransaction, shouldCommitOnGlobalRollbackOnly, suspend, triggerBeforeCommit, triggerBeforeCompletion, useSavepointForNestedTransaction |
| Method from org.springframework.jms.connection.JmsTransactionManager Detail: |
public void afterPropertiesSet() {
if (getConnectionFactory() == null) {
throw new IllegalArgumentException("Property 'connectionFactory' is required");
}
}
Make sure the ConnectionFactory has been set. |
protected Connection createConnection() throws JMSException {
return getConnectionFactory().createConnection();
}
|
protected Session createSession(Connection con) throws JMSException {
return con.createSession(true, Session.AUTO_ACKNOWLEDGE);
}
|
protected void doBegin(Object transaction,
TransactionDefinition definition) {
if (definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT) {
throw new InvalidIsolationLevelException("JMS does not support an isolation level concept");
}
JmsTransactionObject txObject = (JmsTransactionObject) transaction;
Connection con = null;
Session session = null;
try {
con = createConnection();
session = createSession(con);
if (logger.isDebugEnabled()) {
logger.debug("Created JMS transaction on Session [" + session + "] from Connection [" + con + "]");
}
txObject.setResourceHolder(new JmsResourceHolder(getConnectionFactory(), con, session));
txObject.getResourceHolder().setSynchronizedWithTransaction(true);
int timeout = determineTimeout(definition);
if (timeout != TransactionDefinition.TIMEOUT_DEFAULT) {
txObject.getResourceHolder().setTimeoutInSeconds(timeout);
}
TransactionSynchronizationManager.bindResource(
getConnectionFactory(), txObject.getResourceHolder());
}
catch (JMSException ex) {
if (session != null) {
try {
session.close();
}
catch (Throwable ex2) {
// ignore
}
}
if (con != null) {
try {
con.close();
}
catch (Throwable ex2) {
// ignore
}
}
throw new CannotCreateTransactionException("Could not create JMS transaction", ex);
}
}
|
protected void doCleanupAfterCompletion(Object transaction) {
JmsTransactionObject txObject = (JmsTransactionObject) transaction;
TransactionSynchronizationManager.unbindResource(getConnectionFactory());
txObject.getResourceHolder().closeAll();
txObject.getResourceHolder().clear();
}
|
protected void doCommit(DefaultTransactionStatus status) {
JmsTransactionObject txObject = (JmsTransactionObject) status.getTransaction();
Session session = txObject.getResourceHolder().getSession();
try {
if (status.isDebug()) {
logger.debug("Committing JMS transaction on Session [" + session + "]");
}
session.commit();
}
catch (TransactionRolledBackException ex) {
throw new UnexpectedRollbackException("JMS transaction rolled back", ex);
}
catch (JMSException ex) {
throw new TransactionSystemException("Could not commit JMS transaction", ex);
}
}
|
protected Object doGetTransaction() {
JmsTransactionObject txObject = new JmsTransactionObject();
txObject.setResourceHolder(
(JmsResourceHolder) TransactionSynchronizationManager.getResource(getConnectionFactory()));
return txObject;
}
|
protected void doResume(Object transaction,
Object suspendedResources) {
JmsResourceHolder conHolder = (JmsResourceHolder) suspendedResources;
TransactionSynchronizationManager.bindResource(getConnectionFactory(), conHolder);
}
|
protected void doRollback(DefaultTransactionStatus status) {
JmsTransactionObject txObject = (JmsTransactionObject) status.getTransaction();
Session session = txObject.getResourceHolder().getSession();
try {
if (status.isDebug()) {
logger.debug("Rolling back JMS transaction on Session [" + session + "]");
}
session.rollback();
}
catch (JMSException ex) {
throw new TransactionSystemException("Could not roll back JMS transaction", ex);
}
}
|
protected void doSetRollbackOnly(DefaultTransactionStatus status) {
JmsTransactionObject txObject = (JmsTransactionObject) status.getTransaction();
txObject.getResourceHolder().setRollbackOnly();
}
|
protected Object doSuspend(Object transaction) {
JmsTransactionObject txObject = (JmsTransactionObject) transaction;
txObject.setResourceHolder(null);
return TransactionSynchronizationManager.unbindResource(getConnectionFactory());
}
|
public ConnectionFactory getConnectionFactory() {
return this.connectionFactory;
}
Return the JMS ConnectionFactory that this instance should manage transactions for. |
public Object getResourceFactory() {
return getConnectionFactory();
}
|
protected boolean isExistingTransaction(Object transaction) {
JmsTransactionObject txObject = (JmsTransactionObject) transaction;
return (txObject.getResourceHolder() != null);
}
|
public void setConnectionFactory(ConnectionFactory cf) {
if (cf instanceof TransactionAwareConnectionFactoryProxy) {
// If we got a TransactionAwareConnectionFactoryProxy, we need to perform transactions
// for its underlying target ConnectionFactory, else JMS access code won't see
// properly exposed transactions (i.e. transactions for the target ConnectionFactory).
this.connectionFactory = ((TransactionAwareConnectionFactoryProxy) cf).getTargetConnectionFactory();
}
else {
this.connectionFactory = cf;
}
}
Set the JMS ConnectionFactory that this instance should manage transactions for. |