我有一个包含两个表,accountsA和accountsB的数据库。我想在同一个事务范围内对这两个数据库表做一些更新,所以我使用了组件,但是当更新accountsB时发生异常时,accountsA的更新正在继续,我需要我的数据库一起进行两个更新或者没有一个。
为了测试事务处理组件是否正常工作,我对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>