spring事务使用

先来一张异常结构图,请在大脑中锁定该图片。

spring事务使用_第1张图片

spring事务要合理使用,否则当出现异常时,会出现部分数据回滚,部分数据已经修改,造成数据不一致性。

错误示例一

要实现的功能:新增订单,先插入订单主表,然后在新增订单详情表。如果有异常,回滚。

 @Transactional //错误(1)
    @Override
    public BaseResultVo insert(OrderDTO dto) {
        Order order = new Order();
        OrderDetailDTO orderDetailDTO = dto.getOrderDetailDTO();
        OrderDetail orderDetail= new OrderDetail();
        BeanUtils.copyProperties(dto, order);
        BeanUtils.copyProperties(orderDetailDTO , orderDetail);
        
        int count = orderDao.insertSelective(order );
        if (count > 0) {
           if(orderDetailDao.insertSelective(orderDetail) <= 0) {
               //错误(2). 直接返回,不抛异常,这样是错的,事务就不会回滚。
               return BaseResultVo.error();
           }
        }
        // 错误(3)默认直接返回成功,也是不对。
        return BaseResultVo.success();
}

错误(1), @Transactional,没有指定回滚类型。

必选显示指定,修改后: @Transactional(rollbackFor = Exception.class)。

因为在@Transactional注解中如果不配置rollbackFor属性,那么事物只会在遇到RuntimeException的时候才会回滚,加上rollbackFor=Exception.class,可以让事物在遇到非运行时异常时也回滚

错误(2),直接返回错误编码。  return BaseResultVo.error();

必选抛出异常,修改后:throw new Exception();

或者抛出自定义的异常:

throw new OrderException(OrderExceptionEnum.add_detail_fail.getValue(),OrderExceptionEnum.add_detail_fail.getId());

错误(3),如果插入不成功,count=0,这样也会直接返回成功。

正确姿势:

 @Transactional(rollbackFor = Exception.class)
    @Override
    public BaseResultVo insert(OrderDTO dto) {
        Order order = new Order();
        OrderDetailDTO orderDetailDTO = dto.getOrderDetailDTO();
        OrderDetail orderDetail= new OrderDetail();
        BeanUtils.copyProperties(dto, order);
        BeanUtils.copyProperties(orderDetailDTO , orderDetail);
        
        if(orderDao.insertSelective(order)<=0) {
            throw new OrderException(OrderExceptionEnum.add_fail.getValue(),OrderExceptionEnum.add_fail.getId());
        }

        if(orderDetailDao.insertSelective(orderDetail) <= 0) {
              throw new OrderException(OrderExceptionEnum.add_detail_fail.getValue(),OrderExceptionEnum.add_detail_fail.getId());
        }

        return BaseResultVo.success();
}

 

异常更多知识点,请移步:

Java:详解Java中的异常(Error与Exception)

你可能感兴趣的:(异常)