Narayana Transaction Manager configuration - Not rolling back

368 views Asked by At

I am trying to configure the narayana transaction manager with camel and spring. I have created two data source objects and inserting two records in to two different databases. I am throwing error after the first insert and excepting to roll back. In the logs it showing that transaction manager has started but not rolling back.

Below is the camel-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Configures the Camel Context-->

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
       
    <bean id="dataSource1" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
        <property name="driverClassName" value="com.arjuna.ats.jdbc.TransactionalDriver" /> 
        <property name="url" value="jdbc:mysql://localhost:3306/jdbctest" /> 
        <property name="username" value="root" />
        <property name="password" value="Govinda@1" /> 
    </bean> 
    
    <bean id="dataSource2" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.arjuna.ats.jdbc.TransactionalDriver" />
        <property name="url" value="jdbc:mysql://localhost:3306/springtransaction1" />
        <property name="username" value="root" />
        <property name="password" value="Govinda@1" />
    </bean>
    
    <bean id="jtaTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
        <property name="transactionManager">
            <bean class="com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionManagerImple" />
        </property>
        <property name="userTransaction">
            <bean class="com.arjuna.ats.internal.jta.transaction.arjunacore.UserTransactionImple" />
        </property>
    </bean>

    <bean id="PROPAGATION_REQUIRED" class="org.apache.camel.spring.spi.SpringTransactionPolicy">
        <property name="transactionManager" ref="jtaTransactionManager" />
        <property name="propagationBehaviorName" value="PROPAGATION_REQUIRED" />
    </bean>
    <bean id="myError"  class="com.rwx.camelspringnarayana.MyError" />
    
  <camelContext xmlns="http://camel.apache.org/schema/spring">
    <!-- here is a sample which processes the input files
         (leaving them in place - see the 'noop' flag)
         then performs content based routing on the message using XPath -->

        <route>
            <from uri="timer:myTimer1?repeatCount=1" />
            <transacted ref="PROPAGATION_REQUIRED"/>
            <log message="timer1 started" />
            <to uri="sql:insert into cars values(1896, 'Jaldut')?dataSource=#dataSource1" />
            <bean ref="myError" />
            <to uri="sql:insert into cars values(1922, 'Rwx Car')?dataSource=#dataSource1" /> 
            <log message="${body}" />
        </route>

        <route>
            <from uri="timer:myTimer2?repeatCount=1" />
            <delay>
                <constant>5000</constant>
            </delay>
            <log message="timer2 started" />
            <to uri="sql:select * from cars where car_no in (1896,1922)?dataSource=#dataSource1" />
            <log message="${body}" />
        </route>
        
        
        <route>
            <from uri="timer:myTimer4?repeatCount=1" />
            <delay>
                <constant>5000</constant>
            </delay>
            <log message="timer 4 started" />
            <to uri="sql:select * from user_tb where id =2 ?dataSource=#dataSource2" />
            <log message="${body}" />
        </route>
                 
        <route>
            <from uri="timer:myTimer3?repeatCount=1" />
            <delay><constant>10000</constant></delay>
            <log message="timer3 started" />
            <to uri="sql:delete from cars where car_no in (1896,1922)?dataSource=#dataSource1" />
            <log message="No of rows deleted are = ${headers.camelSqlUpdateCount}" />
        </route>
        
        <route>
            <from uri="timer:myTimer5?repeatCount=1" />
            <delay><constant>10000</constant></delay>
            <log message="timer 5started" />
            <to uri="sql:delete from user_tb where id =2?dataSource=#dataSource2" />
            <log message="No of rows deleted are = ${headers.camelSqlUpdateCount}" />
        </route>
        
  </camelContext>

</beans>

**It is not rolling back. even though exception is thrown. I want it to rollback. we see one record is added. and it is deleted which means that it is now rolling back ** [] [el (camel-1) thread #8 - Delay] route2 INFO [{car_no=1896, carName=Jaldut}] [l (camel-1) thread #10 - Delay] route5 INFO timer 5started [el (camel-1) thread #9 - Delay] route4 INFO timer3 started [l (camel-1) thread #10 - Delay] route5 INFO No of rows deleted are = 0 [el (camel-1) thread #9 - Delay] route4 INFO No of rows deleted are = 1

1

There are 1 answers

1
Zheng Feng On

It looks like you need to use jdbc:arjuna: as a prefix for the jdbc url propery. So try to use

<bean id="dataSource1" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
    <property name="url" value="jdbc:mysql://localhost:3306/jdbctest" /> 
    <property name="username" value="root" />
    <property name="password" value="Govinda@1" /> 
</bean>
<bean id="xaDataSource1" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.arjuna.ats.jdbc.TransactionalDriver" />
    <property name="XADATASOURCE" ref="dataSource1"/>
    <property name="url" value="jdbc:arjuna:"/>
    <property name="username" value="root" />
    <property name="password" value="Govinda@1" />    
</bean>

and use #xaDataSource1 in the camel route define.