Spring and local JMS transactions
Posted by danielmeyer on July 1, 2008
Here’s some stuff about how you apparently* use Spring with local JMS transactions enabled.
Sending messages
For sending messages, it looks like you’d just call setSessionTransacted(true) on the jmsTemplate instance. Seems simple enough.
Receiving messages
To enable local JMS transactions on the receiving end, looks like you just call setSessionTransacted(true) on the DefaultMessageListenerContainer. So I guess you’d have something like this in your Spring beans file:
<bean id="myListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="concurrentConsumers" value="${consumerThreadCount}" />
<property name="connectionFactory" ref="jmsConnectionFactory" />
<property name="destination" ref="myQueue" />
<property name="messageListener" ref="myQueueListener" />
<property name="sessionTransacted" value="true" />
</bean>
Hey, look, that’s just what the example in the Spring manual JMS chapter’s section on Transactions shows!
Managed vs. Unmanaged Transactions
There’s another concept here: managed vs. unmanaged transactions. The above example is how you’d enable transactions for an unmanaged session (which I think in Spring means you have not set the transactionManager property on your messagelistenercontainer.) The Spring manual, Section 19.4.5. Processing messages within transactions, speaks to this as well.
Rollbacks
So… how do you rollback
- a message in a transacted but un-transaction-managed JMS session in Spring?
- a message in a transaction-managed JMS session in Spring?
Guess we’ll need a third installment. :)
*I haven’t actually tried any of this, I’m just looking through the Spring manual.