spring Transactional的propagation理解

Transactional的propagation属性区别

  • REQUIRED:默认,只会有一个事务,无则创建,有则使用
  • REQUIRES_NEW:每次都创建一个事务
  • NESTED:在当前事务内嵌事务

需要注意的是,两个方法必须在两个不同的类中,不然回滚机制会有影响
假定有serviceB中有个方法B,包含jdbc操作,serviceA中有个方法A,包含jdbc操作,同时调用serviceB的方法B

REQUIRED:只会有一个事务,无则创建,有则使用
  1. testB有异常,testA捕获了异常,则testB回滚,testA不会滚,如果testA没捕获,则testA也回滚
@Transactional
@Override
public Result testA() {
    testMapper.insert(new Test(UUId.getUUId())); // 因为testB有异常,不管对testB是否有try/catch,testA都会被回滚
    try {
        testService.testB();
    } catch (RuntimeException e) {
        e.printStackTrace();
    }
    return MyResult.success();
}

@Transactional(propagation = Propagation.REQUIRED)
@Override
public int testB() {
    testMapper.insert(new Test(UUId.getUUId())); // 此操作会回滚
    throw new RuntimeException();
}
  1. testA有异常,testB无异常,A和B会一起回滚
@Transactional
@Override
public Result testA() {
    testMapper.insert(new Test("testA")); // 此操作由于该方法有异常会回滚
    testService.testB();
    throw new RuntimeException();
}

@Transactional(propagation = Propagation.REQUIRED)
@Override
public int testB() {
    return testMapper.insert(new Test("testB")); // 此操作由于testA有异常则也会回滚
}
REQUIRES_NEW:每次都创建一个事务
  1. testB有异常,testA捕获了异常,则testB回滚,testA不会滚,如果testA没捕获,则testA也回滚
@Transactional
@Override
public Result testA() {
    testMapper.insert(new Test(UUId.getUUId())); // 此操作会成功插入数据库,因为此方法没有异常,已捕获,如果没有捕获,那么此操作也会被回滚
    try {
        testService.testB();
    } catch (RuntimeException e) {
        e.printStackTrace();
    }
    return MyResult.success();
}

@Transactional(propagation = Propagation.REQUIRES_NEW)
@Override
public int testB() {
    testMapper.insert(new Test(UUId.getUUId())); // 此操作会回滚
    throw new RuntimeException();
}
  1. testA有异常,testB无异常,A和B会一起回滚
@Transactional
@Override
public Result testA() {
    testMapper.insert(new Test("testA")); // 此操作由于该方法有异常会回滚
    testService.testB();
    throw new RuntimeException();
}

@Transactional(propagation = Propagation.REQUIRES_NEW)
@Override
public int testB() {
    return testMapper.insert(new Test("testB")); // 此操作会成功插入数据库
}
NESTED:在当前事务内嵌事务
  1. testB有异常,testA捕获了异常,则testB回滚,testA不会滚,如果testA没捕获,则testA也回滚
@Transactional
@Override
public Result testA() {
    testMapper.insert(new Test(UUId.getUUId())); // 此操作会成功插入数据库,因为此方法没有异常,已捕获,如果没有捕获,那么此操作也会被回滚
    try {
        testService.testB();
    } catch (RuntimeException e) {
        e.printStackTrace();
    }
    return MyResult.success();
}

@Transactional(propagation = Propagation.NESTED)
@Override
public int testB() {
    testMapper.insert(new Test(UUId.getUUId())); // 此操作会回滚
    throw new RuntimeException();
}
  1. testA有异常,testB无异常,A和B会一起回滚
@Transactional
@Override
public Result testA() {
    testMapper.insert(new Test("testA")); // 此操作由于该方法有异常会回滚
    testService.testB();
    throw new RuntimeException();
}

@Transactional(propagation = Propagation.NESTED)
@Override
public int testB() {
    return testMapper.insert(new Test("testB")); // 此操作由于testA有异常则也会回滚
}

宁静致远!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值