Fuse 6 is no longer supported
As of February 2025, Red Hat Fuse 6 is no longer supported. If you are using Fuse 6, please upgrade to Red Hat build of Apache Camel.6.5. The XA Enlistment Problem
The problem of XA enlistment Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
The standard JTA approach to enlisting XA resources is to add the XA resource explicitly to the current
javax.transaction.Transaction
object (representing the current transaction). In other words, you must explicitly enlist an XA resource every time a new transaction starts.
How to enlist an XA resource Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
Enlisting an XA resource with a transaction simply involves invoking the
enlistResource()
method on the Transaction interface. For example, given a TransactionManager
object and an XAResource
object, you could enlist the XAResource
object as follows:
Auto-enlistment Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
The tricky aspect of enlisting resources is that the resource must be enlisted on every new transaction and the resource must be enlisted before you start to use the resource. If you enlist resources explicitly, you could end up with error-prone code that is littered with
enlistResource()
calls. Moreover, sometimes it can be difficult to call enlistResource()
in the right place (for example, if you are using a framework that hides some of the transaction details).
For these reasons, it is much easier (and safer) in practice to use features that support auto-enlistment of XA resources. For example, in the context of using JMS and JDBC resources, the standard technique is to use wrapper classes that support auto-enlistment.
JMS XA wrapper Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
The way to perform auto-enlisting of a JMS XA resource is to implement a wrapper class of type,
javax.jms.ConnectionFactory
. You can then implement this class, so that every time a new connection is created, the JMS XA resource is enlisted with the current transaction.
Apache ActiveMQ provides the wrapper class,
XaPooledConnectionFactory
, which implements auto-enlistment for you and, additionally, implements JMS connection pooling for better performance. To use the XaPooledConnectionFactory
wrapper class, simply create an instance that takes a reference to an XA connection factory instance (for example, ActiveMQXAConnectionFactory
) and also provide a reference to the transaction manager (which is used to enlist the resource).
For example, the following example shows how you can use Spring XML to define an auto-enlisting JMS connection factory (with the bean ID,
jmsXaPoolConnectionFactory
):