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();,这样就可以回滚成功。

相关文章
|
缓存 Java 应用服务中间件
Spring Boot配置优化:Tomcat+数据库+缓存+日志,全场景教程
本文详解Spring Boot十大核心配置优化技巧,涵盖Tomcat连接池、数据库连接池、Jackson时区、日志管理、缓存策略、异步线程池等关键配置,结合代码示例与通俗解释,助你轻松掌握高并发场景下的性能调优方法,适用于实际项目落地。
2237 5
|
JavaScript NoSQL Java
基于SpringBoot+Vue实现的大学生就业服务平台设计与实现(系统源码+文档+数据库+部署等)
面向大学生毕业选题、开题、任务书、程序设计开发、论文辅导提供一站式服务。主要服务:程序设计开发、代码修改、成品部署、支持定制、论文辅导,助力毕设!
|
JavaScript NoSQL Java
基于SpringBoot+Vue的班级综合测评管理系统设计与实现(系统源码+文档+数据库+部署等)
✌免费选题、功能需求设计、任务书、开题报告、中期检查、程序功能实现、论文辅导、论文降重、答辩PPT辅导、会议视频一对一讲解代码等✌
|
JavaScript NoSQL Java
基于SpringBoot+Vue实现的大学生体质测试管理系统设计与实现(系统源码+文档+数据库+部署)
面向大学生毕业选题、开题、任务书、程序设计开发、论文辅导提供一站式服务。主要服务:程序设计开发、代码修改、成品部署、支持定制、论文辅导,助力毕设!
|
前端开发 Java 数据库连接
Java后端开发-使用springboot进行Mybatis连接数据库步骤
本文介绍了使用Java和IDEA进行数据库操作的详细步骤,涵盖从数据库准备到测试类编写及运行的全过程。主要内容包括: 1. **数据库准备**:创建数据库和表。 2. **查询数据库**:验证数据库是否可用。 3. **IDEA代码配置**:构建实体类并配置数据库连接。 4. **测试类编写**:编写并运行测试类以确保一切正常。
1077 2
|
SQL 数据库 索引
【YashanDB数据库】大事务回滚导致其他操作无法执行,报错YAS-02016 no free undo blocks
大事务回滚导致其他操作无法执行,报错YAS-02016 no free undo blocks
|
Java 关系型数据库 数据库连接
使用 Spring Boot 执行数据库操作:全面指南
使用 Spring Boot 执行数据库操作:全面指南
2678 1
|
JavaScript NoSQL Java
基于SpringBoot+Vue实现的冬奥会科普平台设计与实现(系统源码+文档+数据库+部署)
面向大学生毕业选题、开题、任务书、程序设计开发、论文辅导提供一站式服务。主要服务:程序设计开发、代码修改、成品部署、支持定制、论文辅导,助力毕设!
|
JavaScript Java 关系型数据库
基于springboot的项目管理系统
本文探讨项目管理系统在现代企业中的应用与实现,分析其研究背景、意义及现状,阐述基于SSM、Java、MySQL和Vue等技术构建系统的关键方法,展现其在提升管理效率、协同水平与风险管控方面的价值。