Spring整理系列(17)————循序漸進了解spring事務管理的幾種方式
阿新 • • 發佈:2019-01-08
先從例項開始。。。。
一、例項基本業務為銀行轉賬,A賬戶向B賬戶轉賬,業務執行過程要保證A、B兩個帳號資料操作同時成功或失敗,此時就需要事務進行控制,基本例項程式碼如下:
轉賬DAO:
public interface AccountDao {
//轉出操作
public void outMoney(String out,double money);
//轉入操作
public void inMoney(String in,double money);
}
轉賬DAO實現類:
/**
* @Description:轉賬DAO實現 ,需要繼承JdbcDaoSupport,配置bean時注入dataSource資料來源,子類即可通過this使用jdbc模版
*/
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
//轉出
@Override
public void outMoney(String out, double money) {
String sql = "update account set money = money - ? where name = ?";
this.getJdbcTemplate().update(sql,money,out);
}
//轉入
@Override
public void inMoney(String in, double money) {
String sql = "update account set money = money + ? where name = ?";
this.getJdbcTemplate().update(sql,money,in);
}
}
轉賬Service服務介面:
public interface AccountService {
public void transferAccount(String out,String in,double money);
}
轉賬Service服務介面實現類:
public class AccountServiceImpl implements AccountService{
//注入DAO層bean
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
/**
* 無事務管理
* 轉賬業務方法
*/
@Override
public void transferAccount(final String out,final String in,final double money) {
accountDao.outMoney(out, money);
try{
int i = 1/0;
}catch(Exception e){
e.printStackTrace();
}
accountDao.inMoney(in, money);
}
}
xml配置檔案:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 引入屬性檔案 -->
<context:property-placeholder location="classpath:jdbc-connect.properties"/>
<!-- 配置資料來源,使用C3P0連線池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 註冊轉賬DAO bean,注入資料來源,操作資料庫 -->
<bean name="accountDao" class="com.test.spring.transaction.demo1.AccountDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 註冊轉賬service bean,注入DAO -->
<bean name="accountService" class="com.test.spring.transaction.demo1.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
</beans>
二、下面開始對上面基本例項,對Service業務層新增事務管理,幾種不同的事務管理方式如下:
1、程式設計式事務管理:為service層注入事務管理模版來操作事務
xml配置檔案:
<!-- 引入屬性檔案 -->
<context:property-placeholder location="classpath:jdbc-connect.properties"/>
<!-- 配置資料來源,使用C3P0連線池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 配置事務管理器:管理資料來源的事務,引入資料來源dataSource -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置事務管理器的模版,簡化事務呼叫操作,模版是對事務管理的模板化,引入事務管理器transactionManager -->
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager"/>
</bean>
<!-- 註冊轉賬DAO bean,注入資料來源,操作資料庫dataSource -->
<bean name="accountDao" class="com.test.spring.transaction.demo2.AccountDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 註冊轉賬service bean,注入DAO,注入事務管理模版(事務定義在service服務層)transactionTemplate -->
<bean name="accountService" class="com.test.spring.transaction.demo2.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
<property name="transactionTemplate" ref="transactionTemplate"/>
</bean>
修改轉賬Service服務介面實現類的轉賬業務方法:
/**
* 使用程式設計式事務管理
*/
@Override
public void transferAccount(final String out,final String in,final double money) {
transactionTemplate.execute(
//使用匿名內部類的方式執行,注意:匿名內部類使用外部引數,外部引數需要宣告為final型別
new TransactionCallback(){
@Override
public Object doInTransaction(TransactionStatus status) {
//執行業務方法
accountDao.outMoney(out, money);
try{
int i = 1/0;
}catch(Exception e){
e.printStackTrace();
}
//執行業務方法
accountDao.inMoney(in, money);
return null;
}
}
);
}
2、宣告式事務管理一:通過攔截器基於service層事務代理的方式管理和操作事務
xml配置檔案:
<!-- 引入屬性檔案 -->
<context:property-placeholder location="classpath:jdbc-connect.properties"/>
<!-- 配置資料來源,使用C3P0連線池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 註冊轉賬DAO bean,注入資料來源,操作資料庫dataSource -->
<bean name="accountDao" class="com.test.spring.transaction.demo3.AccountDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 註冊轉賬service bean,注入DAO,注入事務管理模版(事務定義在service服務層)transactionTemplate -->
<bean name="accountService" class="com.test.spring.transaction.demo3.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
<!-- 配置事務管理器:管理資料來源的事務,引入資料來源dataSource -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置Service層事務代理物件,對業務物件進行事務方面的曾強處理 -->
<bean id="accountServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<!-- 增強的service目標物件 -->
<property name="target" ref="accountService"/>
<!-- 使用的事務管理器 -->
<property name="transactionManager" ref="transactionManager"/>
<!-- 注入事務屬性:比如事務的隔離級別、傳播級別、異常處理等 -->
<property name="transactionAttributes">
<props>
<prop key="transferAccount">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
恢復轉賬Service服務介面實現類的轉賬業務方法為開始狀態:
@Override
public void transferAccount(final String out,final String in,final double money) {
accountDao.outMoney(out, money);
try{
int i = 1/0;
}catch(Exception e){
e.printStackTrace();
}
accountDao.inMoney(in, money);
}
3、宣告式事務管理二:基於AOP配置的通知性事務管理
xml配置檔案:
<!-- 引入屬性檔案 -->
<context:property-placeholder location="classpath:jdbc-connect.properties"/>
<!-- 配置資料來源,使用C3P0連線池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 註冊轉賬DAO bean,注入資料來源,操作資料庫dataSource -->
<bean name="accountDao" class="com.test.spring.transaction.demo4.AccountDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 註冊轉賬service bean,注入DAO,注入事務管理模版(事務定義在service服務層)transactionTemplate -->
<bean name="accountService" class="com.test.spring.transaction.demo4.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
<!-- 配置事務管理器:管理資料來源的事務,引入資料來源dataSource -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置事務性通知,以切面通知的方式對業務進行事務曾強處理 -->
<tx:advice id="accountAdvice" transaction-manager="transactionManager">
<!-- 配置事務屬性,比如事務的隔離級別、傳播級別、異常處理等 -->
<tx:attributes>
<tx:method name="transferAccount" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 配置切面 -->
<aop:config>
<aop:pointcut id="accountPointcut" expression="execution(* com.test.spring.transaction.demo4.AccountServiceImpl.transferAccount(..))"/>
<aop:advisor advice-ref="accountAdvice" pointcut-ref="accountPointcut"/>
</aop:config>
恢復轉賬Service服務介面實現類的轉賬業務方法為開始狀態:
@Override
public void transferAccount(final String out,final String in,final double money) {
accountDao.outMoney(out, money);
try{
int i = 1/0;
}catch(Exception e){
e.printStackTrace();
}
accountDao.inMoney(in, money);
}
4、宣告式事務管理三:基於AspectJ註解的事務管理
xml配置檔案:
<!-- 引入屬性檔案 -->
<context:property-placeholder location="classpath:jdbc-connect.properties"/>
<!-- 配置資料來源,使用C3P0連線池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 註冊轉賬DAO bean,注入資料來源,操作資料庫dataSource -->
<bean name="accountDao" class="com.test.spring.transaction.demo5.AccountDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 註冊轉賬service bean,注入DAO,注入事務管理模版(事務定義在service服務層)transactionTemplate -->
<bean name="accountService" class="com.test.spring.transaction.demo5.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
<!-- 配置事務管理器:管理資料來源的事務,引入資料來源dataSource -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 基於AspectJ註解的事務管理驅動事務管理器 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
修改轉賬Service服務介面實現類,為其新增@Transactional事務註解,服務方法不變:
@Transactional
public class AccountServiceImpl implements AccountService{
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public void transferAccount(final String out,final String in,final double money) {
accountDao.outMoney(out, money);
try{
int i = 1/0;
}catch(Exception e){
e.printStackTrace();
}
accountDao.inMoney(in, money);
}
}
總結: Spring配置檔案中關於事務配置總是由三個組成部分,分別是DataSource、TransactionManager和代理機制這三部分,無論哪種配置方式,一般變化的只是代理機制這部分。
DataSource、TransactionManager這兩部分只是會根據資料訪問方式有所變化,比如使用Hibernate進行資料訪問時,DataSource實際為SessionFactory,TransactionManager的實現為HibernateTransactionManager。
盜圖如下:
通過上面的例項也可以發現,從操作事務到交給事務管理器,經歷了模版方式、攔截器代理方式、AOP配置切面通知方式、AspectJ註解方式,無論哪一種,它們都是通過配置拿到事務管理器,然後對Service業務方法進行曾強事務處理。