Spring5学习总结(四)spring事务编程式事务管理和声明式事务管理基于注解实现声明式事务管理@Transactional参数配置完全注解开发基于xml配置文件方式实现声明式事务管理

Spring5学习总结(四)spring事务/编程式事务管理和声明式事务管理/基于注解实现声明式事务管理/@Transactional参数配置/完全注解开发/基于xml配置文件方式实现声明式事务管理

一、事务概念

(一)什么是事务?

事务是数据库操作最基本单元,事务由单独单元的一个或多个SQL语句组成,在这个单元中,每个SQL语句是相互依赖的。而整个单独单元作为一个不可分割的整体,如果单元中某条SQL语句一旦执行失败或产生错误,整个单元将会回滚。所有受到影响的数据将返回到事物开始以前的状态;如果单元中的所有SQL语句均执行成功,则事物被顺利执行。

(二)事务的ACID属性

1.原子性(Atomicity)

原子性是指事务是一个不可分割的工作单位,事务中的操作要么都发生,要么都不发生

2.一致性(Consistency)

事务必须使数据库从一个一致性状态变换到另外一个一致性状态。

3.隔离性(Isolation)

事务的隔离性是指一个事务的执行不能被其他事务干扰,即一个事务内部的操作及使用的数据对并发的其他事务是隔离的,并发执行的各个事务之间不能互相干扰。
如果有两个事务,运行在相同的时间内,执行相同的功能,事务的隔离性将确保每一事务在系统中认为只有该事务在使用系统。这种属性称为串行化,为了防止事务操作间的混淆,必须串行化或序列化请求,使得在同一时间仅有一个事务请求同一数据。不同的事务之间彼此没有任何干扰。

4.持久性(Durability)

持久性是指一个事务一旦被提交,它对数据库中数据的改变就是永久性的,接下来的其他操作和数据库故障不应该对其有任何影响。

二、spring事务操作

(一)问题引入

1.假设我有一张数据表account,小明和小红银行账户各有1000元,小明想向小红转账100元。
在这里插入图片描述
2.IDEA中导入jar包
在这里插入图片描述
3.创建外部属性文件,properties 格式文件,写数据库信息

prop.driverClass=com.mysql.jdbc.Driver
prop.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
prop.username=root
prop.password=123456

4.创建 service 层和 dao 层的接口和对象,完成对象创建和注入关系(service 注入 dao,在 dao 注入 JdbcTemplate,在 JdbcTemplate 注入 DataSource),并写spring配置文件
UserDao:

public interface UserDao {
    //加钱操作
    public void addMoney(String name,double money);
    
    //减钱操作
    public void reduceMoney(String name,double money);
}

UserDaoImpl:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class UserDaoImpl implements UserDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void addMoney(String name, double money) {
        String sql="update account set money=money+? where username=?";
        jdbcTemplate.update(sql, money, name);
    }

    @Override
    public void reduceMoney(String name, double money) {
        String sql="update account set money=money-? where username=?";
        jdbcTemplate.update(sql, money, name);
    }
}

UserService:

import com.fox.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    private UserDao userDao;

    //转账
    public void transfer(){
        //小明少100
        userDao.reduceMoney("小明",100);
        //小红多100
        userDao.addMoney("小红",100);
    }
}

<beans xmlns="http://www.springframework.org/schema/beans"
       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/context http://www.springframework.org/schema/context/spring-context.xsd">

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

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

    <!-- 数据库连接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <property name="url" value="${prop.url}" />
        <property name="username" value="${prop.username}" />
        <property name="password" value="${prop.password}" />
        <property name="driverClassName" value="${prop.driverClass}" />
    </bean>

    <!-- JdbcTemplate 对象 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <!--注入上面写好的dataSource-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>
</beans>

5.测试:

import com.fox.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        UserService userService = context.getBean("userService", UserService.class);
        userService.transfer();
    }
}

在这里插入图片描述
这就实现了一次转账操作,但是如果这个过程中出现了异常:

    //转账
    public void transfer(){
        //小明少100
        userDao.reduceMoney("小明",100);
        //模拟异常
        int i=1/0;
        //小红多100
        userDao.addMoney("小红",100);
    }

转账结果:
在这里插入图片描述
小明少了100,而小红并没有多100,那么该怎么解决这个问题呢
使用事务进行解决!
在这里插入图片描述
以上这种是编程式事务管理。在 Spring 出现以前,编程式事务管理对基于 POJO 的应用来说是唯一选择。用过 Hibernate 的人都知道,我们需要在代码中显式调用beginTransaction()commit()rollback()等事务管理相关的方法,这就是编程式事务管理。但令人头疼的是,事务管理的代码散落在业务逻辑代码中,破坏了原有代码的条理性,并且每一个业务方法都包含了类似的启动事务、提交/回滚事务的样板代码,使业务代码变得十分的冗杂。因此我们不采用这种方式,而是用声明式事务管理

(二)声明式事务管理

1.介绍

  • 事务通常添加到 JavaEE 三层结构里面的 Service 层(业务逻辑层)

  • 声明式事务管理有

    两种方式

    • 基于注解方式(推荐使用)
    • 基于 xml 配置文件方式
  • Spring 的声明式事务管理在底层是建立在 AOP 的基础之上的。其原理是对方法前后进行拦截,然后在目标方法开始之前创建或者加入一个事务,在执行完目标方法之后根据执行情况提交或者回滚事务。

  • 声明式事务最大的优点就是不需要通过编程的方式管理事务,这样就不需要在业务逻辑代码中掺杂事务管理的代码,只需在配置文件中做相关的事务规则声明(或通过等价的基于标注的方式),便可以将事务规则应用到业务逻辑中,使得纯业务代码不被污染,极大方便后期的代码维护。因为事务管理本身就是一个典型的横切逻辑,正是 AOP 的用武之地。

  • 和编程式事务相比,声明式事务唯一不足的地方是,后者的最细粒度只能作用到方法级别,无法做到像编程式事务那样可以作用到代码块级别。但是即便有这样的需求,也存在很多变通的方法,比如,可以将需要进行事务管理的代码块独立为方法等等。

  • Spring 提供了一个接口 PlatformTransactionManager,代表事务管理器,这个接口针对不同的框架提供了不同的实现类:
    在这里插入图片描述

2.基于注解实现声明式事务管理

(1)案例演示

还是以上面小明给小红转账100为例:
1.在spring配置文件配置事务管理器并开启事务注解(注意引入tx名称空间 )

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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/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">

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

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

    <!-- 数据库连接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <property name="url" value="${prop.url}" />
        <property name="username" value="${prop.username}" />
        <property name="password" value="${prop.password}" />
        <property name="driverClassName" value="${prop.driverClass}" />
    </bean>

    <!-- JdbcTemplate 对象 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <!--注入上面写好的dataSource-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

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

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

2.在 service 类上面(或者 service 类里面的方法上面)添加事务注解
(1)将@Transactional,这个注解添加到类上面,也可以添加到方法上面
(2)如果把这个注解添加到类上面,这个类里面所有的方法都添加事务
(3)如果把这个注解添加到方法上面,只是为这个方法添加事务

import com.fox.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UserService {
    @Autowired
    private UserDao userDao;

    //转账
    public void transfer(){
        //小明少100
        userDao.reduceMoney("小明", 100);
        //模拟异常
        int i = 1 / 0;
        //小红多100
        userDao.addMoney("小红", 100);
    }
}

最后测试,发现当有异常时,事务回退了,小明没扣钱,小红也没加钱,这是我们想要的结果。
在这里插入图片描述

(2)@Transactional参数配置

在@Transactional注解里面可以配置事务相关参数:

①propagation:事务传播行为

事务传播行为表示多事务方法之间进行调用时,这个过程事务是如何进行管理的
在这里插入图片描述
七种传播行为:
在这里插入图片描述

② ioslation:事务隔离级别
  • 事务有隔离性,多事务操作之间不会产生影响。对于同时运行的多个事务, 当这些事务访问数据库中相同的数据时,如果没有采取必要的隔离机制,就会导致各种并发问题:
    • 脏读:对于两个事务 T1、 T2,T1 读取了已经被 T2 更新但还没有被提交的字段。之后,若 T2 回滚,T1读取的内容就是临时且无效的。
    • 不可重复读: 对于两个事务T1、T2,T1 读取了一个字段,然后 T2 更新了该字段。之后,T1再次读取同一个字段,值就不同了。
    • 幻读:对于两个事务T1、T2,T1 从一个表中读取了一个字段,然后 T2 在该表中插入了一些新的行。之后,如果 T1 再次读取同一个表,就会多出几行。
  • 一个事务与其他事务隔离的程度称为隔离级别。数据库规定了多种事务隔离级别,不同隔离级别对应不同的干扰程度,隔离级别越高,数据一致性就越好,但并发性越弱。
  • 数据库提供的四种事务隔离级别:(MySQL默认的事务隔离级别为:REPEATABLE READ)
隔离级别描述脏读不可重复读幻读
READ UNCOMMITTED(读未提交数据)允许事务读取未被其他事务提交的变更会出现会出现会出现
READ COMMITTED(读已提交数据)只允许事务读取已经被其它事务提交的变更不会出现会出现会出现
REPEATABLE READ(可重复读)确保事务可以多次从一个字段中读取相同的值,在这个事务持续期间,禁止其他事务对这个字段进行更新不会出现不会出现会出现
SERIALIZABLE(串行化)确保事务可以从一个表中读取相同的行,在这个事务持续期间,禁止其他事务对该表执行插入、更新和删除操作。但性能十分低下。不会出现不会出现不会出现
③timeout:超时时间
  • 事务需要在一定时间内进行提交,如果超时不提交则进行回滚
  • 默认值是 -1 ,设置时间以秒为单位
④readOnly:是否只读
  • 读:查询操作,写:添加、修改、删除操作
  • readOnly 默认值 false,表示可以查询,可以添加修改删除操作
  • 设置 readOnly 值是 true,只能查询
⑤rollbackFor:回滚
  • 设置出现哪些异常时进行事务回滚
⑥noRollbackFor:不回滚
  • 设置出现哪些异常时不进行事务回滚

案例演示:

@Service
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.REPEATABLE_READ,timeout = 5,readOnly = false,rollbackFor = ArithmeticException.class)
public class UserService {
}
(3)声明式事务管理的完全注解开发

首先,我们要了解一下 @Bean 注解:

  • Spring的@Bean注解用于告诉方法,产生一个Bean对象,然后这个Bean对象交给Spring管理(和xml配置中的bean标签的作用是一样的)。产生这个Bean对象的方法Spring只会调用一次,随后这个Spring将会将这个Bean对象放在自己的IoC容器中
  • SpringIoC 容器管理一个或者多个bean,这些bean都需要在@Configuration注解下的类中进行创建

对照着上面例子中的spring配置文件,我们的配置类要做如下工作替换配置文件:开启注解扫描、引入外部文件创建数据库连接池、创建 JdbcTemplate 对象、创建事务管理器

配置类:

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

@Configuration//声明这是一个配置类
@ComponentScan(basePackages = {"com.fox"})//开启此包下注解扫描
@EnableTransactionManagement//开启事务
public class MyConfig {
    //创建数据库连接池
    @Bean
    public DruidDataSource getDruidDataSource() throws IOException {
        //用类加载器读取配置文件中的4个基本信息
        InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");

        //创建属性集合类,用于载入配置文件的属性数据
        Properties p = new Properties();
        p.load(is);

        //获取配置文件中的值
        String username = p.getProperty("prop.username");
        String password = p.getProperty("prop.password");
        String url = p.getProperty("prop.url");
        String driverClass = p.getProperty("prop.driverClass");

        //创建德鲁伊数据库连接池
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }

    //创建JdbcTemplate对象
    @Bean
    public JdbcTemplate getJdbcTemplate(DataSource dataSource) {//到ioc容器中根据类型找到dataSource
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        //注入dataSource
        jdbcTemplate.setDataSource(dataSource);
        return jdbcTemplate;
    }

    //创建事务管理器
    @Bean
    public DataSourceTransactionManager getDataSourceTransactionManager(DataSource dataSource) {//到ioc容器中根据类型找到dataSource
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource);
        return transactionManager;
    }
}

测试类(要换成AnnotationConfigApplicationContext):

import com.fox.config.MyConfig;
import com.fox.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
        UserService userService = context.getBean("userService", UserService.class);
        userService.transfer();
    }
}

3.基于xml配置文件方式实现声明式事务管理

(1)在 spring 配置文件中进行配置
第一步、配置事务管理器
第二步、配置通知
第三步、配置切入点和切面(Spring 的声明式事务管理在底层是建立在 AOP 的基础之上的。其原理是对方法前后进行拦截,然后在目标方法开始之前创建或者加入一个事务,在执行完目标方法之后根据执行情况提交或者回滚事务。)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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 https://www.springframework.org/schema/aop/spring-aop.xsd">

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

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

    <!-- 数据库连接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <property name="url" value="${prop.url}" />
        <property name="username" value="${prop.username}" />
        <property name="password" value="${prop.password}" />
        <property name="driverClassName" value="${prop.driverClass}" />
    </bean>

    <!-- JdbcTemplate 对象 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <!--注入上面写好的dataSource-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--1、创建事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--注入数据源-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--2、配置通知-->
    <tx:advice id="txadvice">
        <!--配置事务参数-->
        <tx:attributes>
            <!--name指定在哪个方法上面添加事务-->
            <tx:method name="transfer" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
            <!--也可以通过这种规则决定哪个方法上面添加事务<tx:method name="trans*"/>-->
        </tx:attributes>
    </tx:advice>

    <!--3、配置切入点和切面-->
    <aop:config>
        <!--配置切入点-->
        <aop:pointcut id="pt" expression="execution(* com.fox.service.UserService.*(..))"/>
        <!--配置切面-->
        <aop:advisor advice-ref="txadvice" pointcut-ref="pt"/>
    </aop:config>

</beans>

(2)测试类:

import com.fox.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        UserService userService = context.getBean("userService", UserService.class);
        userService.transfer();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值