java Narayana事务管理器配置-不回滚

py49o6xq  于 2023-01-11  发布在  Java
关注(0)|答案(1)|浏览(160)

我正在尝试用camel和spring配置narayana事务管理器。我已经创建了两个数据源对象,并将两条记录插入到两个不同的数据库中。我在第一次插入后抛出错误,并希望回滚。在日志中,它显示事务管理器已经启动,但没有回滚。
下面是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>

即使抛出异常,它也不会回滚。我希望它回滚。我们看到添加了一条记录。它已删除,这意味着它现在正在回滚[] [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

mutmk8jj

mutmk8jj1#

看起来您需要使用jdbc:arjuna:作为jdbcurl属性的前缀。

<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>

并且在 Camel 式路由定义中使用#xaDataSource1

相关问题