Spring and local JMS transactions

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.

Advertisements

, ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s