Spring框架爲我們提供了一組事務控制的API,本文在此總結下如何通過XML方式和註解方式,進行配置事務控制。
Spring 中事務控制的 API 介紹
PlatformTransactionManager
此接口是 spring 的事務管理器,它裏面提供了我們常用的操作事務的方法,如下圖:
我們在開發中都是使用它的實現類,如下圖:
TransactionDefinition
它是事務的定義信息對象,裏面有如下方法:
事務的隔離級別
事務的傳播行爲
超時時間
默認值是-1,沒有超時限制。如果有,以秒爲單位進行設置。
是否是隻讀事務
建議查詢時設置爲只讀。
TransactionStatus
此接口提供的是事務具體的運行狀態,方法介紹如下圖:
導入依賴
我們需要在pom.xml
文件中引入事務控制和AOP的相關依賴,例如:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
基於XML的聲明式事務控制
相關配置
我們需要在resources
目錄下的spring配置文件中,添加有關Spring事務控制的XML配置內容。
首先,引入相關的命名空間和約束:
<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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>
配置步驟
1、配置事務管理器
2、配置事務的通知
使用tx:advice標籤配置事務通知
屬性:
id:給事務通知起一個唯一標識
transaction-manager:給事務通知提供一個事務管理器引用
3、配置AOP中的通用切入點表達式
4、建立事務通知和切入點表達式的對應關係
5、配置事務的屬性
在事務的通知tx:advice標籤的內部
代碼示例:
<!-- 配置事務管理器 -->
<!-- dataSource爲自己配置的數據源 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事務的通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 配置事務的屬性
isolation:用於指定事務的隔離級別。默認值是DEFAULT,表示使用數據庫的默認隔離級別。
propagation:用於指定事務的傳播行爲。默認值是REQUIRED,表示一定會有事務,增刪改的選擇。查詢方法可以選擇SUPPORTS。
read-only:用於指定事務是否只讀。只有查詢方法才能設置爲true。默認值是false,表示讀寫。
timeout:用於指定事務的超時時間,默認值是-1,表示永不超時。如果指定了數值,以秒爲單位。
rollback-for:用於指定一個異常,當產生該異常時,事務回滾,產生其他異常時,事務不回滾。沒有默認值。表示任何異常都回滾。
no-rollback-for:用於指定一個異常,當產生該異常時,事務不回滾,產生其他異常時事務回滾。沒有默認值。表示任何異常都回滾。
-->
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" read-only="false"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"></tx:method>
</tx:attributes>
</tx:advice>
<!-- 配置aop-->
<aop:config>
<!-- 配置切入點表達式-->
<aop:pointcut id="pt1" expression="execution(* com.itheima.service.impl.*.*(..))"></aop:pointcut>
<!--建立切入點表達式和事務通知的對應關係 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor>
</aop:config>
基於註解的聲明式事務控制
創建 spring 的配置文件導入約束並配置掃描的包
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置 spring 創建容器時要掃描的包 -->
<context:component-scan base-package="com.itheima"></context:component-scan>
<!-- 開啓 spring 對註解事務的支持 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
配置步驟
1、配置事務管理器
2、開啓spring對註解事務的支持
3、在需要事務支持的地方使用@Transactional註解
第一步:配置事務管理器並注入數據源
<!-- 配置事務管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
第二步:在業務層使用@Transactional 註解
例如:
/**
* 賬戶的業務層實現類
*
* 事務控制應該都是在業務層
*/
@Service("accountService")
@Transactional(propagation= Propagation.SUPPORTS,readOnly=true)//只讀型事務的配置
public class AccountServiceImpl implements IAccountService{
@Autowired
private IAccountDao accountDao;
@Override
public Account findAccountById(Integer accountId) {
return accountDao.findAccountById(accountId);
}
//需要的是讀寫型事務配置
@Transactional(propagation= Propagation.REQUIRED,readOnly=false)
@Override
public void transfer(String sourceName, String targetName, Float money) {
System.out.println("transfer....");
//2.1根據名稱查詢轉出賬戶
Account source = accountDao.findAccountByName(sourceName);
//2.2根據名稱查詢轉入賬戶
Account target = accountDao.findAccountByName(targetName);
//2.3轉出賬戶減錢
source.setMoney(source.getMoney()-money);
//2.4轉入賬戶加錢
target.setMoney(target.getMoney()+money);
//2.5更新轉出賬戶
accountDao.updateAccount(source);
int i=1/0;
//2.6更新轉入賬戶
accountDao.updateAccount(target);
}
}
該註解的屬性和 xml 中的屬性含義一致。該註解可以出現在接口上,類上和方法上。 出現接口上,表示該接口的所有實現類都有事務支持。
出現在類上,表示類中所有方法有事務支持 出現在方法上,表示方法有事務支持。 以上三個位置的優先級:方法>類>接口
第三步:在配置文件中開啓 spring 對註解事務的支持
<!-- 開啓spring對註解事務的支持-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
不使用 xml 的配置方式
加入一個配置類就OK。
@Configuration
@EnableTransactionManagement
public class SpringTxConfiguration {
//裏面配置數據源,配置 JdbcTemplate,配置事務管理器。在之前的步驟已經寫過了。
}