Spring 声明式事务详解

!! 观看本篇文章需要配合 [JdbcTemplate 基本使用] https://www.cnblogs.com/orginly/p/15349219.html

Spring 中的事务控制方式

Spring 的事务控制可以分为编程式事务控制声明式事务控制
编程式
开发者直接把事务的代码和业务代码耦合到一起,在实际开发中不用。
声明式
开发者采用配置的方式来实现的事务控制,业务代码与事务代码实现解耦合,使用的 API 思想。

基于 XML 的声明式事务控制【重点】

在 Spring 配置文件中声明式的处理事来代替代码式的处理事务。底层采用 AOP 思想来实现的。

声明式事务控制明确事项:

  • 核心业务代码 (目标对象)
  • 事务增强代码 {Spring 已提供事务管理器)
  • 切面配置

步骤分析

1. 引入 tx 命名空间
2. 事务管理器通知配置
3. 事务管理器 AOP 配置

引入依赖坐标

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd">
</beans>

基于 XML 的声明式事务的控制

配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd">

<!-- 开启 IOC 注解扫描 -->
<context:component-scan base-package="com.orginly"/>

<!-- 引入 properties 文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>

<!-- 配置数据源 -->
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>

<!-- jdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!-- 绑定数据源 -->
<property name="dataSource" ref="datasource"/>
</bean>

<!-- 事务管理器对象 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 数据源对象 -->
<property name="dataSource" ref="datasource"/>
</bean>

<!-- 通知增强 transaction-manager: 配置的事务管理器对象 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 定义一些事务属性 -->
<tx:attributes>
<!-- * 表示当前任意名称的方法都走默认的配置 -->
<tx:method name="*"/>
</tx:attributes>
</tx:advice>

<!-- AOP 配置切面 -->
<aop:config>
<!-- 配置声明式式务 -->
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.orginly.service.impl.UserServiceImpl.updateUser(..))"/>
</aop:config>
</beans>

Service 层

1
2
3
4
5
6
7
8
9
10
@Override
public void updateUser() {
User user = userDao.find(2);
user.setName("li3");
userDao.updateUser(user);
int i = 1 / 0;
user.setAge(13);
userDao.updateUser(user);
System.out.println(user);
}

事务参数的配置详解

1
<tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" timeout="-1" read-only="false"/>
  • name:切点方法等
  • isolation:事务的隔离级别
  • propogation:事务的传播行为
  • timeout:超时时间
  • read-only:是否只读

常用 CURD 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 定义一些事务属性 -->
<tx:attributes>

<!-- CRUD 常用配置 表示以update开头的方法-->
<tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" timeout="-1"/>
<tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" timeout="-1"/>
<tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" timeout="-1"/>
<tx:method name="find" read-only="true"/>
<!-- * 表示当前任意名称的方法都走默认的配置 -->
<tx:method name="*"/>
</tx:attributes>
</tx:advice>

基于注解的声明式事务控制

常用注解

步骤分析

  1. 修改 Service 层,增加事务注解

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.REPEATABLE_READ,readOnly = false,timeout = -1)
    public void updateUser() {
    User user = userDao.find(2);
    user.setName("li3");
    userDao.updateUser(user);
    int i = 1 / 0;
    user.setAge(13);
    userDao.updateUser(user);
    System.out.println(user);

    }
  2. 修改 Spring 核心配置文件,开启事务注解支持

    1
    <tx:annotation-driven/>