spring Transactional注解

来源:互联网 发布:宁泽涛傅园慧cp乐乎 编辑:程序博客网 时间:2024/06/07 20:43
/** * @author 27238 */@Service@Transactionalpublic class ProductInfoServiceImpl implements ProductInfoService {    private final ProductInfoRepository productInfoRepository;    @Override    public ProductInfo save(ProductInfo productInfo) throws Exception {        ProductInfo save = productInfoRepository.save(productInfo);        if (true) {            throw new Exception();        }        return save;    }}


调用方法抛出Exception,发现并没有回滚依然修改了数据库

需要指定异常类型,spring默认遇到RuntimeException事物回滚

/** * @author 27238 */@Service@Transactional(rollbackFor = Exception.class)public class ProductInfoServiceImpl implements ProductInfoService {    private final ProductInfoRepository productInfoRepository;    @Override    public ProductInfo save(ProductInfo productInfo) throws Exception {        ProductInfo save = productInfoRepository.save(productInfo);        if (true) {            throw new Exception();        }        return save;    }}

原创粉丝点击