• 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:在这里插入图片描述

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

      • 同成功同失败!!!

  • 相关阅读:
    ppt太大怎么压缩变小?ppt压缩方法和步骤
    [Bug]Ubuntu下使用TexStudio存在的一些问题
    【Java】lambda表达式,Stream API,函数式编程接口
    计算机组成原理---第三章存储系统---双端口RAM和多模块存储器
    吐血整理如何在Google Earth Engine上写循环 五个代码实例详细拆解
    (附源码)计算机毕业设计SSM教师职称资料管理系统
    LeetCode·每日一题·1704.判断字符串的两半是否相似·双指针
    virtualbox中ubuntu22.04网络配置
    Publish Over SSH插件的使用(jenkins远程服务器发送文件)
    入手瑞芯微RK3588
  • 原文地址:https://blog.csdn.net/ALVIS_108/article/details/126612002