service层事务回滚@Transactional(rollbackFor = Exception.class)

PS:经过本人亲自验证,把注解加在类上是没有用的,加在方法上就好使了,具体原因不知道为啥

现在用的框架:springboot+mybatis+vue

 

注解@Transaactional(),需要设置rollbackFor属性,不是单纯加上注解就行,一定要加属性

事务场景中,抛出异常被catch后,如果需要回滚,一定要手动回滚事务

有三种方式进行回滚,一种是在类上加注解,一种是在方法上添加,一种是try catch后回滚

1:在类上加注解

@Service
@Transactional(rollbackFor = Exception.class)
public class ManagementSubscribeServiceImpl{

    @Override
    public boolean saveSettings(SettingsBo settingsBo) {
    return managementSubscribeMapper.saveSettings(settingsBo);
}
}

2:在方法上加注解

@Service

public class ManagementSubscribeServiceImpl{

@Override

@Transactional(rollbackFor = Exception.class)

public boolean saveSettings(SettingsBo settingsBo) {

return managementSubscribeMapper.saveSettings(settingsBo);

}
}

3:catch到异常后回滚

service层事务回滚@Transactional(rollbackFor = Exception.class)_第1张图片

 

你可能感兴趣的:(service层事务回滚@Transactional(rollbackFor = Exception.class))