• springboot 事务的使用


    @Transactional
    public JsonUtil secondInstoreConfirm(@RequestParam(value = "taskId") String taskId) {
    JsonUtil json = JsonUtil.newInstance();
    try {
    Task task = taskService.createTaskQuery().taskId(taskId).includeProcessVariables().singleResult();
    if (task == null)
    return JsonUtil.error("审批任务未找到");
    String userId = getCurrentUserId();
    //测试数据
    // userId = "2BDE7CB62A438BEF7EC73ADE194B44CB";
    if (task.getAssignee().equals(userId) == false)
    return JsonUtil.error("对不起,您没有审批权限");

    //出库
    List<StockUpdate> stockUpdateEntityList = getInStoreApplyEntityForm(taskId);
    //执行仓库的出库操作
    json= outWarehouseService.outBound(stockUpdateEntityList, userId);
    if(json.getCode()!=0)
    {
    throw new RuntimeException();
    // return json;
    }
    Map<String, Object> map = new HashMap<String, Object>();
    // map.put("stockUpdateEntityList", stockUpdateEntityList);

    System.out.println("开始入库");

    // String secondStoreId = storeRecordDao.findRecordIdByTicketAndLevel(userId, "二级");
    String secondStoreId =(String)task.getProcessVariables().get("needInStoreroomId");

    String outNo = (String) taskService.getVariable(taskId, "outNo");
    //把仓库变成需要入库的二级库
    for (int i = 0; i < stockUpdateEntityList.size(); i++) {
    stockUpdateEntityList.get(i).setStoreroomId(secondStoreId);
    }
    boolean rst = enterWarehouseService.notLevelOneStorage(stockUpdateEntityList, userId, outNo);
    if(rst==false){
    throw new RuntimeException();
    // return JsonUtil.error(StockConstants.SERVER_ERROR_TIP);
    }

    // map.put("inStockEntityList", stockUpdateEntityList);
    taskService.complete(taskId, map);

    notifyApplyerExt( task,"确认入库完成");

    return JsonUtil.success("二级仓库管理员确认入库完成");
    } catch (Exception e) {
    e.printStackTrace();
    throw e;
    }

    }

    这个函数,包括两步骤,一级库出库,二级库入库。这是一个事务操作。一旦发生异常需要回滚。
    (1)需要在catch分支中抛出异常。一旦发生异常。就会回滚。
    (2)try分支中,原来出错判断,是执行return json,现在改为抛出异常。进入catch分支。然后throw e,然后@Tranactional就会发挥作用。
    是不是很简单!!!




  • 相关阅读:
    js命名空间
    window安装node.js
    JS添加可信站点、修改ActiveX安全设置,禁用弹出窗口阻止程序的方法
    Javascript 操作select控件大全(新增、修改、删除、选中、清空、判断存在等)
    DIV+CSS两种盒子模型
    table中的tbody标签
    兼容获取元素的样式属性值
    Arch linux 使用心得
    763. 划分字母区间
    <Leetcode>93. 复原地址
  • 原文地址:https://www.cnblogs.com/zhanying999666/p/9667523.html
Copyright © 2020-2023  润新知