分佈式事務:一個事務涉及到要去操作位於不同服務器上的資源(數據庫),這時就要去保證每個數據庫裏面的狀態一致,如果出現異常還要去不同的數據庫裏面回滾。
這裏舉一個實際的例子就是 同時修改兩個數據庫的表中內容
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());
}
}