【体系-单体架构】13-Spring 的事务管理

1.Spring 事务管理简介

问题:单个方法里面操作2次及以上次数数据库,无法保证业务的原子性。例如:当进行删除分类操作时,内容删除报错,那么此时该操作就应该回滚到操作前的状态,此时就应该用Spring事务管理进行优化(相对于JDBC事务管理进行升级)
在这里插入图片描述
在这里插入图片描述
概念:回顾之前所讲的业务复杂度问题

  • 简单业务:单业务里面操作数据库1次
  • 普通业务:单业务里面操作数据库3次
  • 复杂业务:单业务里面操作数据库7次

简介:事务原本是数据库中的概念,用于数据访问层。但一般情况下,需要将事务提升到业务层,即 Service 层。这样做是为了能够使用事务的特性来管理具体的业务。在 Spring 中通常可以通过以下两种方式来实现对事务的管理:使用 Spring 的事务注解管理事务(注解配置)和使用 AspectJ 的 AOP 配置管理事务(xml配置)

事务管理器接口:事务管理器是 PlatformTransactionManager 接口对象。其主要用于完成事务的提交、回滚,及获取事务的状态信息。该接口定义了 3 个事务方法:void commit(TransactionStatus status,事务的提交)、TransactionStatus getTransaction(TransactionDefinition definition,获取事务的状态)、void rollback(TranscationStatus status,事务的回滚)

常用的两个实现类:PlatformTransactionManager 接口有两个常用的实现类:DataSourceTransactionManager使用 JDBC 或 MyBatis 进行持久化数据时使用、HibernateTransactionManager使用 Hibernate 进行持久化数据时使用

Spring 的回滚方式:发生运行时异常回滚(注意:该异常不能编写try…catch…语句进行处理,该报错是交由Spring进行统一管理)

事务定义接口:事务定义接口 TransactionDefinition 中定义了事务描述相关的三类常量:事务隔离级别、事务传播行为、事务默认超时时限(默认无超时时间限制),及对它们的操作

事务的四种隔离级别

  • DEFAULT:采用 DB 默认的事务隔离级别。MySql 默认为 REPEATABLE_READ;Oracle 默认为:READ_COMMITTED;
  • READ_UNCOMMITTED:读未提交。未解决任何并发问题。
  • READ_COMMITTED:读已提交。解决脏读,存在不可重复读与幻读。
  • REPEATABLE_READ:可重复读。解决脏读、不可重复读。存在幻读。
  • SERIALIZABLE:串行化。不存在并发问题。

并发问题

类型事务A事务B
脏读A.a()B.b()
读取 table id=1
修改 table id=2
读取 table id=2
未提交,报错回滚
读取 table id=1
不可重复读A.a()B.b()
读取 table id=1
修改 table id=2
提交 commit
读取 table id=2
幻读A.a()B.b()
读取 table.size=10
add table 1 record
读取 table.size=11

2.使用 AspectJ 的 AOP 配置管理事务

概述:AspectJ 主要是使用 XML 配置顾问方式自动为每个符合切入点表达式的类生成事务代理
步骤

<!--1. 在pom.xml文件中添加org.springframework:spring-aspects依赖-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>${spring.version}</version>
</dependency>

<!--2. 在spring-context.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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.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">

    <context:annotation-config/>
    <context:component-scan base-package="com.hello.spring.transaction.aspectsj.aop">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 配置事务通知 -->
    <tx:advice id="myAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 以save开头的方法名都要用到传播机制为REQUIRED -->
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="select*" propagation="NEVER"/>
            <tx:method name="get*" propagation="NEVER"/>
        </tx:attributes>
    </tx:advice>

    <!-- 配置顾问和切入点 -->
    <aop:config>
        <!--切入点:指定需要切入的方法,对象是方法-->
        <aop:pointcut id="myPointcut" expression="execution(* com.demo.spring.transaction.aspectsj.aop.service.*.*(..))" />
        <!--顾问:指定需要加的事-->
        <aop:advisor advice-ref="myAdvice" pointcut-ref="myPointcut" />
    </aop:config>
</beans>

3.使用 Spring 注解管理事务

概述:通过 @Transactional 注解方式,也可将事务织入到相应方法中。而使用注解方式,只需在配置文件中加入一个 tx 标签,以告诉 Spring 使用注解来完成事务的织入。该标签只需指定一个属性,事务管理器

<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>

<!-- 开启事务注解驱动 -->
<tx:annotation-driven transaction-manager="transactionManager" />

在这里插入图片描述
@Transactional 注解简介:以下是可选属性

  • propagation:用于设置事务传播属性。该属性类型为 Propagation 枚举,默认值为 Propagation.REQUIRED。
  • isolation:用于设置事务的隔离级别。该属性类型为 Isolation 枚举 ,默认值为 Isolation.DEFAULT。
  • readOnly:用于设置该方法对数据库的操作是否是只读的。该属性为 boolean,默认值为 false。
  • timeout:用于设置本操作与数据库连接的超时时限。单位为秒,类型为 int,默认值为 -1,即没有时限。
  • rollbackFor:指定需要回滚的异常类。类型为 Class[],默认值为空数组。当然,若只有一个异常类时,可以不使用数组。
  • rollbackForClassName:指定需要回滚的异常类类名。类型为 String[],默认值为空数组。当然,若只有一个异常类时,可以不使用数组。
  • noRollbackFor:指定不需要回滚的异常类。类型为 Class[],默认值为空数组。当然,若只有一个异常类时,可以不使用数组。
  • noRollbackForClassName: 指定不需要回滚的异常类类名。类型为 String[],默认值为空数组。当然,若只有一个异常类时,可以不使用数组。

注意:@Transactional 若用在方法上,只能用于 public 方法上。对于其他非 public 方法,如果加上了注解 @Transactional,虽然 Spring 不会报错,但不会将指定事务织入到该方法中。因为 Spring 会忽略掉所有非 public 方法上的 @Transaction 注解。若 @Transaction 注解在类上,则表示该类上所有的方法均将在执行时织入事务

地址Architecture-MVC-myshop Spring 事务管理功能源码 tag:3.3.1-RELEASE

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值