分布式事务:一个事务涉及到要去操作位于不同服务器上的资源(数据库),这时就要去保证每个数据库里面的状态一致,如果出现异常还要去不同的数据库里面回滚。
这里举一个实际的例子就是 同时修改两个数据库的表中内容
1. 配置两个datasource,利用JDBC模板方法配置两个JdbcTemplate
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- jotm 本地实例 -->
<bean id="jotm" class="org.springframework.transaction.jta.JotmFactoryBean" />
<!-- JTA事务管理器 -->
<bean id="txManager"
class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="userTransaction" ref="jotm"></property>
</bean>
<!-- XAPool配置,内部包含了一个XA数据源,对应Database1数据库 -->
<bean id="db1" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource"
destroy-method="shutdown">
<property name="dataSource">
<!-- 内部XA数据源 -->
<bean class="org.enhydra.jdbc.standard.StandardXADataSource"
destroy-method="shutdown">
<property name="transactionManager" ref="jotm" />
<property name="driverName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://192.168.1.28:3306/Database1?useUnicode=true&characterEncoding=UTF-8" />
</bean>
</property>
<property name="user" value="root" />
<property name="password" value="123456" />
</bean>
<!-- 另一个XAPool配置,内部包含另一个XA数据源,对应Database2数据库 -->
<bean id="db2" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource"
destroy-method="shutdown">
<property name="dataSource">
<bean class="org.enhydra.jdbc.standard.StandardXADataSource"
destroy-method="shutdown">
<property name="transactionManager" ref="jotm" />
<property name="driverName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://192.168.1.28:3306/Database2?useUnicode=true&characterEncoding=UTF-8" />
</bean>
</property>
<property name="user" value="root" />
<property name="password" value="123456" />
</bean>
<!-- 配置访问Database1数据源的Spring JDBC模板 -->
<bean id="ds1" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="db1"></property>
</bean>
<!-- 配置访问Database2数据源的Spring JDBC模板 -->
<bean id="ds2" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="db2"></property>
</bean>
</beans>
- 在分布式事务中使用这两个datasource
@Resource(name = "txManager")
private JtaTransactionManager txManager;
protected JdbcTemplate ds1_jdbcTemplate;
protected JdbcTemplate ds2_jdbcTemplate;
然后再具体的method里面:
UserTransaction userTx = this.txManager.getUserTransaction();
try{
userTx.begin();
ds1_jdbcTemplate.execute("update account set money='1300' where accountid=8");
ds2_jdbcTemplate.execute("update account set money='1700' where accountid=8");
userTx.commit();
}
catch(Exception e){
System.out.println("捕获到异常,进行回滚" + e.getMessage());
e.printStackTrace();
try{
userTx.rollback();
}catch (IllegalStateException e1) {
System.out.println("IllegalStateException:" + e1.getMessage());
}catch (SecurityException e1) {
System.out.println("SecurityException:" + e1.getMessage());
}catch (SystemException e1) {
System.out.println("SystemException:" + e1.getMessage());
}
}