其他分享
首页 > 其他分享> > flowable: parent 'xxxxxx' was updated by another transaction concurrently

flowable: parent 'xxxxxx' was updated by another transaction concurrently

作者:互联网

先看下我的代码:

    /**
     * 认领
     * @param request
     * @return
     */
    @Transactional
    public ReturnVo claim(ClaimRequest request){
        // 流程实例ID
        String procId = request.getProcessInstanceId();
        // 查询实例上的所有呼叫人员
        StartCallerRequest startCallerRequest = new StartCallerRequest();
        startCallerRequest.setProcessInstanceId(procId);
        List<StartCallersModel> callers = startCallerMapper.query(startCallerRequest);
        // 把没传递过来的呼叫人员删除(删除活动)
        List<String> deleteUserIds = new ArrayList<>();
        for (StartCallersModel caller: callers) {
            boolean exist = false;
            for (String userId: request.getUserIds()) {
                if(userId.equals(caller.getUserId())){
                    exist = true;
                    break;
                }
            }
            if(!exist){
                deleteUserIds.add(caller.getUserId());
            }
        }

        //查询需要删除的活动(任务),并删除
        if (deleteUserIds.size() > 0){
            List<org.flowable.task.api.Task> tasks = taskService.createTaskQuery().processInstanceId(procId)
                    .taskDefinitionKey("claim").taskAssigneeIds(deleteUserIds).list();
            for (org.flowable.task.api.Task task: tasks) {
                try{
                    Execution execution =  runtimeService.createExecutionQuery().executionId(task.getExecutionId()).singleResult();
                    runtimeService.deleteMultiInstanceExecution(execution.getId(), false);
                }
                catch(Exception e){
                    throw e;
                }
            }
        }

        // 查询需要认领的任务并自动审批通过
        List<org.flowable.task.api.Task> tasks1 = taskService.createTaskQuery().processInstanceId(procId).taskDefinitionKey("claim").list();
        for (org.flowable.task.api.Task task : tasks1) {
            //设置流程执行人
            identityService.setAuthenticatedUserId(task.getAssignee());
            //添加批注
            taskService.addComment(task.getId(), procId, TaskEnum.Agree.toString(), "事件认领");
            //完成任务
            Map<String,Object> paras = new HashMap<>();
            paras.put("assignee", task.getAssignee());
            taskService.complete(task.getId(), paras);
        }

        //更新认领状态
        for (String userId : request.getUserIds()) {
            //构造查询条件
            startCallerRequest = new StartCallerRequest();
            startCallerRequest.setProcessInstanceId(procId);
            startCallerRequest.setUserId(userId);

            List<StartCallersModel> models = startCallerMapper.query(startCallerRequest);
            if(models.size() > 0){
                StartCallersModel model = models.get(0);
                model.setClaimed(true);
                startCallerMapper.update(model);
            }
        }

        //删除[执行认领]中的任务
        if (deleteUserIds.size() > 0){
            List<org.flowable.task.api.Task> tasks = taskService.createTaskQuery().processInstanceId(procId)
                    .taskDefinitionKey("execute_claim").taskAssigneeIds(deleteUserIds).list();
            for (org.flowable.task.api.Task task: tasks) {
                Execution execution =  runtimeService.createExecutionQuery().executionId(task.getExecutionId()).singleResult();
                runtimeService.deleteMultiInstanceExecution(execution.getId(), false);
            }
        }

        return new ReturnVo(ReturnCode.SUCCESS,"执行认领成功");
    }

先说下我的需求:

1.有一个节点:[执行认领]

2.有10个人,默认情况下10个人都动态分配到了该节点

3.那么假如有5/6/7个人执行该节点,允许审批通过

4.所以我要把剩余的5/4/3个人从节点中动态删除

5.然后就遇到了该问题

 

解决方案:添加 @Transactional 注解。至于原因我也不知道,被我给懵对了。

标签:procId,updated,transaction,认领,List,startCallerRequest,task,xxxxxx,deleteUserIds
来源: https://www.cnblogs.com/subendong/p/15810122.html