首页 > 其他分享 >A调用B方法,@Transactional事务问题

A调用B方法,@Transactional事务问题

时间:2022-10-19 14:48:10浏览次数:86  
标签:事务 调用 Transactional 回滚 entity OcResourceFileEntity AB public

总结:
方法A调用方法B:
1、如果只有A加@Transactional注解;则AB在同一事务中,任意异常都回滚;
2、如果只有B加@Transactional注解;AB方法为同一类,事务失效任意异常都不回滚;AB不同类,只有B有事务且只有B异常B才回滚;

测试:

@RestController
@RequestMapping("test")
@Slf4j
public class TestController {

    @GetMapping("/test")
    public R test() {
        testA();
        return R.success();
    }

    @Autowired
    ResourceFileService resourceFileService;

    public void testA() {
        OcResourceFileEntity entity = new OcResourceFileEntity();
        entity.setApplyId("11111");
        resourceFileService.save(entity);
        testB();
    }
    @Transactional(rollbackFor = Exception.class)
    public void testB() {
        OcResourceFileEntity entity = new OcResourceFileEntity();
        entity.setApplyId("22222");
        resourceFileService.save(entity);
        int i = 1/0;
    }
}

 

标签:事务,调用,Transactional,回滚,entity,OcResourceFileEntity,AB,public
From: https://www.cnblogs.com/min225016/p/16806147.html

相关文章