apache-camel - 不能在 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>
解决方案
也许您只是省略了它,但是您的问题中缺少的一个关键要素是DataSourceTransactionManager
使您具有DataSource
事务性。
<bean id="jdbcTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="myDataSource"/>
</bean>
推荐阅读
- java - 每次发出请求时都会处理 karate-configure.js 吗?
- r - 用 igraph 创建的图的颜色节点与该节点的特征向量中心性成比例
- ansible - ZABBIX AWX,同步库存
- php - 是否可以在不创建目录的情况下创建子链接。使用 PHP
- asp.net-web-api - 无法从 NuGet 包管理器安装 Microsoft.AspNet.WebApi.Cors
- c# - 强制查询字符串数组参数以样式形式处理并爆炸为假的推荐方法是什么
- python - pyautoGUI安装报错 CMD报错
- javascript - 如何抓取由 chunk-vendors.js 插入的结构化数据
- reactjs - 将 React 组件显示为字符串
- angular-cli - Angular Schematics 跳过“实现 OnInit”