首页 > 解决方案 > 不能在 apache 骆驼中工作

问题描述

我有一个包含两个表accountA 和accountB 的数据库。我想在同一个事务范围内对这两个数据库表进行一些更新,所以我使用了组件,但是当更新帐户 B 时发生异常时,帐户 A 的更新正在继续,我需要我的数据库一起进行两个更新或他们都没有。

为了测试事务处理组件是否正常工作,我更改了 accountB 表名的名称,产生了异常。我原以为 accountA 表的更新会停止,但它没有发生。我做错什么了吗?

    <bean id="mysql-ds-local" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  <property name="url" value="jdbc:mysql://localhost:3306/BankDB?relaxAutoCommit=true"/>
  <property name="username" value="root"/>
  <property name="password" value="osslab"/>
  <property name="poolPreparedStatements" value="true"/>
    </bean>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="mysql-ds-local"/>
</bean>


<camelContext id="camel-jdbc-test" xmlns="http://camel.apache.org/schema/blueprint" >

   <route id="main-route-jdbc">


      <from uri="timer://webinar?period=20000" /> 
       <transacted />
          <to uri="direct:reduceCredit"/>
          <to uri="direct:increaseCredit"/>
    </route>
    <route id="reduceCredit-route">
        <from uri="direct:reduceCredit"/>
        <log message="in direct accountA"/>
        <setBody>
            <constant>update accountsA set credit = credit + 1 where id = 1</constant>
        </setBody>
        <to uri="jdbc:mysql-ds-local" />
    </route>
   <route id="increaseCredit-route">
        <from uri="direct:increaseCredit"/>
        <log message="in direct accountB"/>
        <setBody>
            <constant>update accountsB set credit = credit + 1 where id = 1</constant>
        </setBody>
        <to uri="jdbc:mysql-ds-local" />
    </route>

</camelContext>

标签: apache-cameltransactionscopetransactional

解决方案


也许您只是省略了它,但是您的问题中缺少的一个关键要素是DataSourceTransactionManager使您具有DataSource事务性。

<bean id="jdbcTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="myDataSource"/>
</bean>

推荐阅读