camel-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Daniel Pocock <>
Subject Camel distributed transactions/XA without full J2EE container?
Date Thu, 04 Dec 2014 15:41:22 GMT

The wiki explains the general concepts around transactions and
distributed transactions.

Running Camel as a standalone J2SE Spring application (using
org.apache.camel.spring.Main) is a popular choice for people who don't
want to use a container and I've worked on several projects where this
type of deployment was desired.

In this scenario, without a container, Camel transactions involving a
single JMS or JDBC provider work using the transaction capabilities of
the provider.

When a distributed transaction is required, Camel relies on Spring's
transaction support and Spring seems to need some standalone JTA
implementation.  This is where it gets tricky.

I managed to get this working on one project using JOTM but it is
awkward (reasons below) and I'm just wondering if there is an example of
a solution that is simpler to implement.

The JOTM-based solution involved the following:
- add xbean-spring, JOTM and Carol JARs to classpath
- include JotmFactoryBean and JtaPersistenceUnitPostProcessor classes in
the project (I don't recall why this was necessary as it appears to
exist in Spring too)
- in the jndi.xml for xbean-spring, create the java:comp/UserTransaction
in JNDI using JotmFactoryBean:

                <entry key="java:comp/UserTransaction">
                    <bean class="org.example.util.JotmFactoryBean" />

   and also create JNDI entries for each DataSource using the bean class

               <entry key="java:comp/env/jdbc/testDS">
class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
                        <property name="driverClass"
value="com.mysql.jdbc.Driver" />
                        <property name="jdbcUrl"
value="jdbc:mysql://localhost:3306/test1" />
                        <property name="user" value="test" />
                        <property name="password" value="123" />
                        <property name="maxIdleTime" value="1800" />

- in the camelContext.xml, create spring beans using the above objects
from JNDI:

    <bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
        <property name="environment">

    <bean id="testDataSource"
class="org.springframework.jndi.JndiObjectFactoryBean" autowire="no">
        <property name="jndiName" value="java:comp/env/jdbc/testDS" />
        <property name="jndiTemplate" ref="jndiTemplate" />

    <bean id="myPersistenceProvider"
class="org.hibernate.ejb.HibernatePersistence" />

    <bean id="pu-test"
        <property name="persistenceXmlLocation"
value="classpath:META-INF/persistence-test.xml" />
        <property name="persistenceUnitName" value="pu-test" />
        <property name="dataSource" ref="testDataSource" />
        <property name="persistenceProvider" ref="myPersistenceProvider" />
        <property name="persistenceUnitPostProcessors">
            <bean class="org.example.util.JtaPersistenceUnitPostProcessor">
                <property name="jtaDataSource" ref="testDataSource" />
        <property name="jpaVendorAdapter">
                <property name="showSql" value="true" />
                <property name="database" value="MYSQL" />
        <property name="jpaProperties">

    <bean id="jotmTransactionManager"
        <property name="userTransaction">
            <jee:jndi-lookup jndi-name="java:comp/UserTransaction"/>

        <property name="transactionManager" ref="jotmTransactionManager" />
        <property name="propagationBehaviorName"

    <tx:annotation-driven transaction-manager="jotmTransactionManager" />

    <bean id="jpa" class="org.apache.camel.component.jpa.JpaComponent">
        <property name="entityManagerFactory" ref="pu-test" />
        <property name="transactionManager" ref="jotmTransactionManager" />

- and also add ActiveMQ with JTA support:

    <bean id="activemq"
        <property name="brokerURL" value="tcp://localhost:61616" />
        <property name="transactionManager" ref="jotmTransactionManager" />

The good news is, this seems to work without any container.

The bad news is, it is not trivial to create the runtime classpath.
- I have a working, hand-crafted runtime classpath based on the specific
project concerned.
- when I tried to put new Camel JARs in the classpath (not changing
anything else or the order of the JARS), JOTM would fail to initialize,
exceptions from Carol
- when I tried to build the project with Maven (it was originally built
with Ant), I can run it successfully with:

       mvn exec:java -Dexec.mainClass=org.apache.camel.spring.Main

   but if I try to run the JVM from a script using the classpath
generated by

       mvn dependency:build-classpath

    then JOTM fails to initialize, once again, giving exceptions from
the Carol JAR

My overall impression is that this is quite tedious to get it right and
can break easily when more dependencies are added, especially if Maven
is helping.  Does anybody have a suggestion or examples, possibly using
another transaction manager?

View raw message