• Day 94


    _Spring技术–事务处理

    1. 事务作用:在数据层保障一系列的数据库操作同成功同失败

    2. Spring事务作用:在数据层或业务层保障一系列的数据库操作同成功同失败

    3. 需求:实现任意两个账户间转账操作

      • 需求微缩:A账户加钱,B账户减钱
      • 分析:
        • 数据层提供基础操作,指定账户减钱(outMoney),指定账户加钱(inMoney)
        • 业务层提供两个账号和操作金额执行转账操作
        • 基于Spring整合Mybatis环境搭建上述操作
    4. 结果分析:

      • 程序正常执行,账户金额A减B加,没有问题
      • 程序出现异常后,转账失败,但是异常之前的操作成功,异常之后的操作失败,整体业务失败
    5. 首先来看样例:

      • 在spring的整体大框架中,实例代码的结构如图所示:在这里插入图片描述

      • 再来看数据库表中两个人的账户余额:在这里插入图片描述

      • 正常情况下将老王的钱转账500给张三

        • package com.Alvis;
          
          import com.Alvis.config.SpringConfig;
          import com.Alvis.service.FundsService;
          import org.junit.Test;
          import org.junit.runner.RunWith;
          import org.springframework.context.ApplicationContext;
          import org.springframework.context.annotation.AnnotationConfigApplicationContext;
          import org.springframework.test.context.ContextConfiguration;
          import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
          
          @RunWith(SpringJUnit4ClassRunner.class)
          @ContextConfiguration(classes = SpringConfig.class)
          
          public class ServiceTest {
              @Test  // 测试转账方法
              public void transferMoney() {
                  ApplicationContext apc = new AnnotationConfigApplicationContext(SpringConfig.class);
                  FundsService fundsService = apc.getBean(FundsService.class);
                  fundsService.transfer(500);
              }
              
          }
          ================================================
          转帐前:
          Property{id=1, name='老王', property=1000}
          Property{id=2, name='张三', property=1000}
          转账后:
          Property{id=1, name='老王', property=500}
          Property{id=2, name='张三', property=1500}
          
          进程已结束,退出代码0
          
          • 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
        • 在这里插入图片描述

        • 但是如果程序中出错,就会只执行了老王转账的操作,并没有执行张三收钱的操作:在这里插入图片描述

        • 执行后的结果为:在这里插入图片描述

      • 解决办法:进行事务管理(三步走)

        • 第一步:在SpringConfig中注解开启事务,告诉Spring有事务需要开启:在这里插入图片描述

        • 第二步:在接口注解需要开启事务的方法:在这里插入图片描述

        • 第三步:在JdbcConfig中对事务管理的第三方的配置进行bean管理,具体来说就是创建新的Bean:在这里插入图片描述

      • 加上事务管理之后再次运行程序:在这里插入图片描述

      • 同成功同失败!!!

  • 相关阅读:
    git 中如何删除 Untracked files 文件
    Java基础学习之JavaScript
    【会议分享】2022年第四届计算机视觉与模式识别国际会议(CCVPR 2022)
    宝塔composer 安装laravel依赖出现的问题
    CADSoftTools CADEditorX v15.0 Crack
    sqlserver 各种集合、区间、 时间轴(持更)
    实现风控中台案例分析
    Spring Cloud Alibaba微服务第7章之负载均衡Ribbon
    shell中通配符的使用
    数据挖掘算法原理与实践:k-均值
  • 原文地址:https://blog.csdn.net/ALVIS_108/article/details/126612002