一个方法里面部分代码块事务处理方式

一个方法里面部分代码块事务处理方式

业务惨景,比如有时候我们在写业务的时候 一般写在service 方法里面,这个时候可能会碰到 你希望 部分业务带事务,部分不需要事务处理(如第3方 接口,通过定时任务什么的去补偿 )。

  public CheckOrderTO createOrder(CheckOrderDTO checkOrderDTO) {
        // 处理参数校验
        CheckOrderTO orderTO = new CheckOrderTO();
        orderTO.setSuccess(true);
        String errorMsg = checkParam(checkOrderDTO);
        if(StringUtils.isNotBlank(errorMsg)){
            orderTO.setMsg(errorMsg);
            orderTO.setSuccess(false);
            return orderTO;
        }
        String channelCode = checkOrderDTO.getChannelCode();
        CheckOrderService checkOrderService = SpringUtil.getBean("checkOrderService");
        String orderSn = checkOrderService.createOrderDB(checkOrderDTO);
        orderTO.setOrderSn(orderSn);
        // 发送预约中消息
        sendOrderMsg(checkOrderDTO, orderSn, CheckOrderStatusEnum.APPOINTMENT_IN_PROGRESS, CheckChannelOrderStatusEnum.APPOINTMENT_IN_PROGRESS);
        if (Objects.equals(SupportChannelEnum.MEI_NIAN.getValue(), channelCode)) {
            // 渠道创建成功
            try {
                PostCreateDTO postCreateDTO = postCreateMeiNianOrder(checkOrderDTO, channelCode, orderSn);
                //更新渠道号 订单状态
                String serviceNumber = postCreateDTO.getServiceNumber();
                checkOrderRepository.updateChannelOrderSnByOrderSn(orderSn, serviceNumber,CheckOrderStatusEnum.APPOINTMENT_SUCCESS,CheckChannelOrderStatusEnum.APPOINTMENT_SUCCESS);
                // 发送成功消息
                sendOrderMsg(checkOrderDTO, orderSn, CheckOrderStatusEnum.APPOINTMENT_SUCCESS, CheckChannelOrderStatusEnum.APPOINTMENT_SUCCESS);
            }catch (SupportException supportException){
                // 渠道正常返回错误异常 不是超时
                log.info("渠道校验异常 {}",supportException);
                checkOrderRepository.updateChannelOrderSnByOrderSn(orderSn, "",CheckOrderStatusEnum.CANCELED,CheckChannelOrderStatusEnum.CANCEL_RESERVATION);
                // 发送取消消息
                sendOrderMsg(checkOrderDTO, orderSn, CheckOrderStatusEnum.CANCELED, CheckChannelOrderStatusEnum.CANCEL_RESERVATION);
            }catch (Exception e){
                // 特殊异常 不做处理 可能连接超时导致 问题
                log.error("其他异常",e);
                throw e;
            }

        }
        
        
 @Transactional(rollbackFor = Exception.class)
    public String createOrderDB(CheckOrderDTO checkOrderDTO) {
        String oldOrderSn = getExistOrderSn(checkOrderDTO);
        if (oldOrderSn != null){
            return oldOrderSn;
        }
        try {
            String orderSn = checkOrderRepository.saveOrder(checkOrderDTO);
            return orderSn;
        }catch (DuplicateKeyException duplicateKeyException){
            log.info("重复调用创建订单 {}",duplicateKeyException);
            String existOrderSn = getExistOrderSn(checkOrderDTO);
            if (existOrderSn != null){
                return existOrderSn;
            }else {
                throw duplicateKeyException;
            }
        }
    }        
posted @ 2023-06-27 17:22  川流不息&  阅读(104)  评论(0编辑  收藏  举报