就同一个Service类中,一个方法调用另外一个有事务的方法的思考

就同一个Service类中,一个方法调用另外一个有事务的方法的思考

写这篇博客的缘由
我在Spring事务管理嵌套事务详解 : 同一个类中,一个方法调用另外一个有事务的方法——levae1024.评论过,有人说我杠,这里就写篇实际操作的博客证明下我的评论
在这里插入图片描述

现在库里的数据如下图所示:
在这里插入图片描述
现在我在同一个Service中一个事务的方法调用另外一个事务方法
目的是 将id为1与2 的记录中的name属性值Jack与Rose 分别改为Tom与Jerry
如下图代码所示:

package com.wqf.service.flowable.impl;

import com.wqf.dao.flowable.FlowableStudentMapper;
import com.wqf.entity.Student;
import com.wqf.service.flowable.FlowableStudentService;
import com.wqf.service.mybatis.MybatisStudentService;
import com.wqf.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Author: Wang Qifan
 * @Date: 2020/5/24 22:58
 * @Description:
 */
@Service
@Transactional(value = Constants.FLOWABLE_TM)
public class FlowableStudentServiceImpl implements FlowableStudentService {

    @Resource
    private FlowableStudentMapper flowableStudentMapper;

    @Autowired
    private MybatisStudentService mybatisStudentService;

    //注入自己
    @Autowired
    private FlowableStudentService flowableStudentService;

    @Transactional
    public void updateStudent(Student student) {
        student = new Student();
        student.setId(1);
        student.setStudentId(3);
        student.setName("Tom");
        System.out.println("first查询1:" + flowableStudentMapper.getStudentById(1));
        System.out.println("first查询2:" + flowableStudentMapper.getStudentById(2));
        flowableStudentMapper.updateStudentById(student);
        try {
            flowableStudentService.updateOtherStudent(null);
        }catch (Exception e){
            //这里用的try catch 吃掉addOtherStudent()的异常, 
            //避免了因为addOtherStudent()抛出的异常 而导致此updateStudent()方法异常,从而导致回滚
            System.out.println("消化addOtherStudent()的异常");
        }
        System.out.println("second查询1:" + flowableStudentMapper.getStudentById(1));
        System.out.println("second查询2:" + flowableStudentMapper.getStudentById(2));
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void updateOtherStudent(Student student) {
        student = new Student();
        student.setId(2);
        student.setStudentId(3);
        student.setName("Jerry");
        flowableStudentMapper.updateStudentById(student);
        //模拟运行时异常,这个肯定会回滚
        int a = 1/0;
    }
    
    @Override
    public Student getStudentById(int id) {
        Student student = flowableStudentMapper.getStudentById(id);
        return student;
    }
    
}

调用测试类:

package com.wqf;

import com.wqf.service.flowable.FlowableStudentService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * Author: Wang Qifan
 * Date: 2021/2/22 15:56
 * Description:
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class PropagationRequiresNewTest {

    @Autowired
    private FlowableStudentService flowableStudentService;

    @Test
    public void test(){
        System.out.println("first查询1:" + flowableStudentService.getStudentById(1));
        System.out.println("first查询2:" + flowableStudentService.getStudentById(2));
        flowableStudentService.updateStudent(null);
        System.out.println("second查询1:" + flowableStudentService.getStudentById(1));
        System.out.println("second查询2:" + flowableStudentService.getStudentById(2));
    }

}

结果如下图所示:
在这里插入图片描述

在这里插入图片描述
分析:这里若是updateStudent()方法和updateOtherStudent()是同一个事务,那么肯定是同时回滚(因为我上面人为得写了一个运行时异常),而实际结果是updateOtherStudent()回滚了,而updateStudent()中得成功了,那么显然易见,他们不是同一个事务。

若是直接调用:
id为1的改为Mary
在这里插入图片描述
他们是同一个事务,读者可自行验证

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 16
    评论
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值