完全弄懂spring中的事务传播机制(required,require_new,nested)

Spring中的事务传播机制

spring中的事务传播机制之前一直不是很懂,这次整理了一下,对于难理解的三种传播机制做了比较分析,这三个在项目中还是比较常用的,建议理解消化,对比记忆。下面先把整理好的表格放上来。主要应对的三种情况的异常:外层正常try-catch内层,内层出错;外层正常,内层出错,外层不try-catch;外层出错,内层正常

在这里插入图片描述

下面我把测试代码块放上来

目录结构

在这里插入图片描述
数据库表结构
在这里插入图片描述

TestMapper

package com.example.demo.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;

import java.util.Map;

@Mapper
public interface TestMapper {
    @Insert("insert into users(username,password,department) values(#{username},#{password},#{department})")
    void register(Map<String, Object> params);
}

InnerServiceImpl

package com.example.demo.service;

import com.example.demo.mapper.TestMapper;
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 java.util.HashMap;
import java.util.Map;

@Service
@Transactional(propagation = Propagation.NESTED)
public class InnerServiceImpl implements InnerService {
    @Autowired
    TestMapper testMapper;

    @Override
    public void test() throws Exception {
        Map<String,Object> params = new HashMap<>();
        params.put("username","inner");
        params.put("password","sd");
        params.put("department","s212");
        testMapper.register(params);
        throw new RuntimeException("错误了");
    }

    @Override
    public void test2() {
        Map<String,Object> params = new HashMap<>();
        params.put("username","inner");
        params.put("password","sd");
        params.put("department","s212");
        testMapper.register(params);
    }
}

TestServiceImpl

package com.example.demo.service;

import com.example.demo.mapper.TestMapper;
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 java.util.HashMap;
import java.util.Map;

@Service
@Transactional(propagation = Propagation.NESTED)
public class TestServiceImpl implements TestService {
    @Autowired
    InnerService innerService;
    @Autowired
    TestMapper testMapper;
    @Override
    public void t2() throws Exception {
        Map<String,Object> params = new HashMap<>();
        params.put("username","outer");
        params.put("password","sd");
        params.put("department","s212");
        testMapper.register(params);
        innerService.test();
    }

    @Override
    public void t3() {
        Map<String,Object> params = new HashMap<>();
        params.put("username","outer");
        params.put("password","sd");
        params.put("department","s212");
        testMapper.register(params);
        try {
            innerService.test();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void t4() throws Exception {
        Map<String,Object> params = new HashMap<>();
        params.put("username","outer");
        params.put("password","sd");
        params.put("department","s212");
        testMapper.register(params);
        innerService.test2();
        throw new RuntimeException("外层出错");
    }
}

测试用例

package com.example.demo;

import com.example.demo.service.TestService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class Demo7ApplicationTests {
    @Autowired
    TestService testService;

    @Test
    void contextLoads() throws Exception {
//        testService.t2();  // 测试内层报错
//        testService.t3();  // 测试内层报错,外层try catch
        testService.t4();  // 测试外层报错
    }
}

修改InnerServiceImpl中@Transactional(propagation = Propagation.NESTED)分别为Propagation.REQUIRED,Propagation.REQUIRE_NEW,然后用测试中的三个方法分别进行测试。查看数据库中的数据,进行分析,就可以得到表格中的结果。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Cofer_Yin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值