0
点赞
收藏
分享

微信扫一扫

Spring中的事务管理详解

三次方 2022-03-11 阅读 83

Spring中的事务管理详解


一、回顾一下事务

  • 事务在项目开发过程非常重要,涉及到数据的一致性的问题,不容马虎!
  • 事务管理是企业级应用程序开发中必备技术,用来确保数据的完整性和一致性。

事务就是把一系列的动作当成一个独立的工作单元,这些动作要么全部完成,要么全部不起作用。

事务的四个属性ACID

  1. 原子性(atomicity)
    事务是原子性操作,由一系列动作组成,事务的原子性确保动作要么全部完成,要么完全不起作用
  2. 一致性(consistency)
    一旦所有事务动作完成,事务就要被提交。数据和资源处于一种满足业务规则的一致性状态中
  3. 隔离性(isolation)
    可能多个事务会同时处理相同的数据,因此每个事务都应该与其他事务隔离开来,防止数据损坏
  4. 持久性(durability)
    事务一旦完成,无论系统发生什么错误,结果都不会受到影响。通常情况下,事务的结果被写到持久化存储器中

二、 测试

在之前的案例中,我们给userMapper接口新增两个方法,删除和增加用户;

public interface UserMapper {
    public List<User> selectUser();
    //添加一个用户
    public int addUser(User user);
    //删除一个用户
    public int deleteUser(int id);
}

UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lding.mapper.UserMapper">
    <select id="selectUser" resultType="user">
        select * from mybatis.user
    </select>

    <insert id="addUser" parameterType="user">
        insert into mybatis.user(id, name, pwd) values (#{id},#{name},#{pwd});
    </insert>
    <delete id="deleteUser" parameterType="int">
        delete from mybatis.user where id=#{id};
    </delete>
</mapper>

UserMapperImpl.java

public class UserMapperImpl extends SqlSessionDaoSupport implements UserMapper{


    @Override
    public List<User> selectUser() {
        User user=new User(10,"小王1","212121");
        SqlSession sqlSession = getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        mapper.addUser(user);
        mapper.deleteUser(5);
        return mapper.selectUser();
    }
    @Override
    public int addUser(User user) {
        return getSqlSession().getMapper(UserMapper.class).addUser(user);
    }

    @Override
    public int deleteUser(int id) {
        return getSqlSession().getMapper(UserMapper.class).deleteUser(id);
    }
}

测试

public class MyTest {
    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserMapper userMapper = context.getBean("userMapper", UserMapper.class);
        List<User> userList = userMapper.selectUser();
        for (User user : userList) {
            System.out.println(user);
        }
    }
}

这个demo主要是测一下事务,我故意将UserMapper.xml中的删除sql语句写错,然后在运行加入一个用户后删除这个用户,我们会发现,删除用户的功能保持,但是加入用户的功能仍然能实现,这样就不符合事务的一致性。我们希望他要么都成功,要么都失败,所以我们要加入事务管理机制。

二、Spring中的事务管理

Spring在不同的事务管理API之上定义了一个抽象层,使得开发人员不必了解底层的事务管理API就可以使用Spring的事务管理机制。Spring支持编程式事务管理和声明式的事务管理。

编程式事务管理

  • 将事务管理代码嵌到业务方法中来控制事务的提交和回滚
  • 缺点:必须在每个事务操作业务逻辑中包含额外的事务管理代码

声明式事务管理

  • 一般情况下比编程式事务好用。
  • 将事务管理代码从业务方法中分离出来,以声明的方式来实现事务管理。
  • 将事务管理作为横切关注点,通过aop方法模块化。Spring中通过Spring AOP框架支持声明式事务管理

我们一般使用声明式管理,让spring自动帮我们管理事务

使用Spring管理事务,注意头文件的约束导入 : tx

xmlns:tx="http://www.springframework.org/schema/tx"
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">

注意:打一个<tx:按万能解错 可以自动导入一个
在这里插入图片描述
但是这里面的尾缀可能不对,要把c改成tx
同时要加上下面两行,可以复制aop的,也要把尾缀改成tx
在这里插入图片描述
事务管理器

  • 无论使用Spring的哪种事务管理策略(编程式或者声明式)事务管理器都是必须的
  • 就是 Spring的核心事务管理抽象,管理封装了一组独立于技术的方法

配置声明式事务

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

配置好事务管理器后我们需要去配置事务的通知

        <tx:attributes>
            <tx:method name="add" propagation="REQUIRED"></tx:method>
            <tx:method name="delete" propagation="REQUIRED"></tx:method>
            <tx:method name="update" propagation="REQUIRED"></tx:method>
            <tx:method name="query" read-only="true"></tx:method>
            <tx:method name="*" propagation="REQUIRED"></tx:method>
        </tx:attributes>
    </tx:advice>

注意,这里的propagation="REQUIRED"是默认的,也可以不写,read-only代表只读,本操作就不能对数据库进行修改

配置事务的切入

    <aop:config>
        <aop:pointcut id="txPointCut" expression="execution(* com.lding.mapper.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"></aop:advisor>
    </aop:config>

注意:这里的com.lding.mapper..(…),第一个代表mapper包下的所有类,第二个
代表任意类中的所有方法,(…)代表每个方法的任意参数

在配置了事务之后,我们在执行相同的操作,发现如果delete不成功,那么前面的adduser操作也不会成功。实现了事务的四大特性。

如果对您有帮助,免费的赞点一个~~~感谢🙏

在这里插入图片描述

举报

相关推荐

0 条评论