springboot数据库回滚失败原因

简介: springboot数据库回滚失败原因

更多ruoyi-nbcio功能请看演示系统

gitee源代码地址

前后端代码: https://gitee.com/nbacheng/ruoyi-nbcio

演示地址:RuoYi-Nbcio后台管理系统

更多nbcio-boot功能请看演示系统

gitee源代码地址

后端代码: https://gitee.com/nbacheng/nbcio-boot

前端代码:https://gitee.com/nbacheng/nbcio-vue.git

在线演示(包括H5) : http://218.75.87.38:9888

今天在用流程启动的时候出现错误,结果相应的事务没有回滚,我主要是用下面语句

@Override
  @Transactional(rollbackFor = Exception.class)
  public R<Void> startProcessByDataId(String dataId, String serviceName, Map<String, Object> variables) {
        //提交审批的时候进行流程实例关联初始化
      
        if (serviceName==null){
             return R.fail("未找到serviceName:"+serviceName);
        }
        WfCustomForm wfCustomForm = wfCustomFormService.selectSysCustomFormByServiceName(serviceName);
        if(wfCustomForm ==null){
           return R.fail("未找到sysCustomForm:"+serviceName);
        }
        //优先考虑自定义业务表是否关联流程,再看通用的表单流程关联表
        ProcessDefinition processDefinition;
        String deployId = wfCustomForm.getDeployId();
        if(StringUtils.isEmpty(deployId)) {
          WfDeployForm sysDeployForm  = deployFormMapper.selectSysDeployFormByFormId("key_"+String.valueOf(wfCustomForm.getId()));
            if(sysDeployForm ==null){           
               return R.fail("自定义表单也没关联流程定义表,流程没定义关联自定义表单"+wfCustomForm.getId());
            }
            processDefinition = repositoryService.createProcessDefinitionQuery()
            .parentDeploymentId(sysDeployForm.getDeployId()).latestVersion().singleResult();
        }
        else {
          processDefinition = repositoryService.createProcessDefinitionQuery()
                .parentDeploymentId(deployId).latestVersion().singleResult();
        }
        try {
          LambdaQueryWrapper<WfMyBusiness> wfMyBusinessLambdaQueryWrapper = new LambdaQueryWrapper<>();
          wfMyBusinessLambdaQueryWrapper.eq(WfMyBusiness::getDataId, dataId);
          WfMyBusiness business = wfMyBusinessService.getOne(wfMyBusinessLambdaQueryWrapper);
          if (business==null){
            if(processDefinition==null) {
              return R.fail("自定义表单也没关联流程定义表,流程没定义关联自定义表单"+wfCustomForm.getId());
            }
            boolean binit = wfCommonService.initActBusiness(wfCustomForm.getBusinessName(), dataId, serviceName, 
            processDefinition.getKey(), processDefinition.getId(), wfCustomForm.getRouteName());
            if(!binit) {
              return R.fail("自定义表单也没关联流程定义表,流程没定义关联自定义表单"+wfCustomForm.getId());
            }
            WfMyBusiness businessnew = wfMyBusinessService.getOne(wfMyBusinessLambdaQueryWrapper);
             //流程实例关联初始化结束
              if (StrUtil.isNotBlank(businessnew.getProcessDefinitionId())){
                return this.startProcessByDefId(businessnew.getProcessDefinitionId(),variables);
              }
              return this.startProcessByDefKey(businessnew.getProcessDefinitionKey(),variables);
          }
          else {
             return R.fail("已经存在这个dataid实例,不要重复申请:"+dataId);
          }
        } catch (Exception e) {
              e.printStackTrace();
              return R.fail("流程启动错误");
        } 
  }

结果实际数据没有回滚。

根据相关资料,由于异常被catch, 不阻断整个事务执行。所以应该是这里出现问题

可以不用try catch,或者就是需要修改如下:

@Override
  @Transactional(rollbackFor = Exception.class)
  public R<Void> startProcessByDataId(String dataId, String serviceName, Map<String, Object> variables) {
        //提交审批的时候进行流程实例关联初始化
      
        if (serviceName==null){
             return R.fail("未找到serviceName:"+serviceName);
        }
        WfCustomForm wfCustomForm = wfCustomFormService.selectSysCustomFormByServiceName(serviceName);
        if(wfCustomForm ==null){
           return R.fail("未找到sysCustomForm:"+serviceName);
        }
        //优先考虑自定义业务表是否关联流程,再看通用的表单流程关联表
        ProcessDefinition processDefinition;
        String deployId = wfCustomForm.getDeployId();
        if(StringUtils.isEmpty(deployId)) {
          WfDeployForm sysDeployForm  = deployFormMapper.selectSysDeployFormByFormId("key_"+String.valueOf(wfCustomForm.getId()));
            if(sysDeployForm ==null){           
               return R.fail("自定义表单也没关联流程定义表,流程没定义关联自定义表单"+wfCustomForm.getId());
            }
            processDefinition = repositoryService.createProcessDefinitionQuery()
            .parentDeploymentId(sysDeployForm.getDeployId()).latestVersion().singleResult();
        }
        else {
          processDefinition = repositoryService.createProcessDefinitionQuery()
                .parentDeploymentId(deployId).latestVersion().singleResult();
        }
        try {
          LambdaQueryWrapper<WfMyBusiness> wfMyBusinessLambdaQueryWrapper = new LambdaQueryWrapper<>();
          wfMyBusinessLambdaQueryWrapper.eq(WfMyBusiness::getDataId, dataId);
          WfMyBusiness business = wfMyBusinessService.getOne(wfMyBusinessLambdaQueryWrapper);
          if (business==null){
            if(processDefinition==null) {
              return R.fail("自定义表单也没关联流程定义表,流程没定义关联自定义表单"+wfCustomForm.getId());
            }
            boolean binit = wfCommonService.initActBusiness(wfCustomForm.getBusinessName(), dataId, serviceName, 
            processDefinition.getKey(), processDefinition.getId(), wfCustomForm.getRouteName());
            if(!binit) {
              return R.fail("自定义表单也没关联流程定义表,流程没定义关联自定义表单"+wfCustomForm.getId());
            }
            WfMyBusiness businessnew = wfMyBusinessService.getOne(wfMyBusinessLambdaQueryWrapper);
             //流程实例关联初始化结束
              if (StrUtil.isNotBlank(businessnew.getProcessDefinitionId())){
                return this.startProcessByDefId(businessnew.getProcessDefinitionId(),variables);
              }
              return this.startProcessByDefKey(businessnew.getProcessDefinitionKey(),variables);
          }
          else {
             return R.fail("已经存在这个dataid实例,不要重复申请:"+dataId);
          }
        } catch (Exception e) {
              e.printStackTrace();
              throw new RuntimeException();
        } 
  }

在catch里扔出throw new RuntimeException();,这样就可以回滚成功。

相关文章
|
1天前
|
JavaScript Java 关系型数据库
学习平台|基于Springboot+vue的学习平台系统的设计与实现(源码+数据库+文档)
学习平台|基于Springboot+vue的学习平台系统的设计与实现(源码+数据库+文档)
12 0
|
1天前
|
JavaScript Java 测试技术
大学生体质测试|基于Springboot+vue的大学生体质测试管理系统设计与实现(源码+数据库+文档)
大学生体质测试|基于Springboot+vue的大学生体质测试管理系统设计与实现(源码+数据库+文档)
9 0
|
1天前
|
JavaScript NoSQL 小程序
特产销售|基于Springboot+vue的藏区特产销售平台(源码+数据库+文档)​
特产销售|基于Springboot+vue的藏区特产销售平台(源码+数据库+文档)​
22 0
|
1天前
|
JavaScript 小程序 Java
班级综合测评|基于Springboot+vue的班级综合测评管理系统(源码+数据库+文档)
班级综合测评|基于Springboot+vue的班级综合测评管理系统(源码+数据库+文档)
11 0
|
1天前
|
JavaScript Java 关系型数据库
“智慧食堂”|基于Springboot+vue的“智慧食堂”系统(源码+数据库+文档)
“智慧食堂”|基于Springboot+vue的“智慧食堂”系统(源码+数据库+文档)
6 0
|
1天前
|
JavaScript 小程序 Java
“漫画之家”|基于Springboot+vue的“漫画之家”系统(源码+数据库+文档)
“漫画之家”|基于Springboot+vue的“漫画之家”系统(源码+数据库+文档)
10 0
|
1天前
|
JavaScript Java 关系型数据库
车辆充电桩|基于Springboot+vue的车辆充电桩管理系统的设计与实现(源码+数据库+文档)
车辆充电桩|基于Springboot+vue的车辆充电桩管理系统的设计与实现(源码+数据库+文档)
13 0
|
5天前
|
关系型数据库 MySQL API
实时计算 Flink版产品使用合集之可以通过mysql-cdc动态监听MySQL数据库的数据变动吗
实时计算Flink版作为一种强大的流处理和批处理统一的计算框架,广泛应用于各种需要实时数据处理和分析的场景。实时计算Flink版通常结合SQL接口、DataStream API、以及与上下游数据源和存储系统的丰富连接器,提供了一套全面的解决方案,以应对各种实时计算需求。其低延迟、高吞吐、容错性强的特点,使其成为众多企业和组织实时数据处理首选的技术平台。以下是实时计算Flink版的一些典型使用合集。
82 0
|
8天前
|
关系型数据库 MySQL 数据库
docker MySQL删除数据库时的错误(errno: 39)
docker MySQL删除数据库时的错误(errno: 39)
64 0
|
7天前
|
Java 关系型数据库 MySQL
【MySQL × SpringBoot 突发奇想】全面实现流程 · xlsx文件,Excel表格导入数据库的接口(下)
【MySQL × SpringBoot 突发奇想】全面实现流程 · xlsx文件,Excel表格导入数据库的接口
46 0