JMS XA Transactions with Atomikos

When considering reliable message delivery in JMS, there are two main categories.

  • Acknowledgements
  • Transactions

JMS Acknowledgements comes in three types,


JMS Transactions are in two types

  • Local Transactions
  • Distributed(XA) Transaction

This post I’m trying to give a brief explanation on XA transactions in JMS and give a sample source code I have used to try out JMS Distributed transactions with ActiveMQ and Atomikos.

In JMS Classic API our approach to send/receive messages is by,

  1. Looking up ConnectionFactory instance
  2. Create Connection using the ConnectionFactory
  3. Create Session using the Connection
  4. Create Producer/Consumer using the Session
  5. Send/Receive Messages

When comes to JMS XA, we have similar sets of objects

  • XAConnectionFactory
  • XAConnection inherited from Connection
  • XASession inherited from Session (single XASession will hold one Session and one XAResource)
  • XAResource

Note: XAResource is used by the Transaction Manager to control the transaction.

So when we are doing an XA transaction following are the main steps to follow,

  1. Lookup for the XAConnectionFactory(s) instance
  2. Create XAConnection(s) object using the XAConnectionFactory(s)
  3. Create XASession(s) using the XAConnection(s)
  4. Create/Locate Distributed transaction manager and (get the transaction) and enlist the XAResource(s)
  5. Begin the transaction
  6. Get Session from the XASession and create Message Producers/Consumers, and send/receive messages
  7. Commit/Rollback tranaction

Following is the sample source code[2] I have used to demonstrate this flow. In the sample I have used only single XAResource and performed a single operation.

You can enlist multiple XAResources to the Transaction Manager and perform transaction, by extending this further.