spring mvc +mybtis 事务处理失败的原因分析

最近做了个复杂的功能,设计多个表多次的插入,更新操作,无奈事务不起作用:

经过查找资料,问同事   导致事务失效原因

1   @transactional 标注的service方法 种写的增删改操作,不能提取出来,写道另一个方法中,虽然可能导致一个方法过大,但是必须这么做,

2 自定义的异常,需要继承自runtimeexception  这个是spring AOP 规定的吧,或者手动回滚

TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()

,否则不能回滚;

@Transactional(value = "aicaseTsManager")
public int importFile(HttpServletRequest request) throws IOException,PointCountException,DataNullException,AiCaseException{
 
{
        int   rowCount=0;
        int count= 0;
        HttpSession session = request.getSession();
        String realPath;
        String sourceCaseId = request.getParameter("targetId");
        if (FrameworkHelper.isEmpty(sourceCaseId)) {
            sourceCaseId = request.getParameter("sourceCaseId");
        }
        if(FrameworkHelper.isEmpty(sourceCaseId)){
            throw new AiCaseException();
        }
        String brotherIdentifier=  caseUtil.getSubIdentifierByPid(sourceCaseId);
        String sourceIdentifier=getIdentifier(sourceCaseId);
        if(FrameworkHelper.isEmpty(sourceIdentifier)){
            throw new RuntimeException();
        }
        //将request变成多部分request
        MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
        //获取multiRequest 中所有的文件名
        Iterator iter = multiRequest.getFileNames();
        while (iter.hasNext()) {
            //一次遍历所有文件
            MultipartFile file = multiRequest.getFile(iter.next().toString());
            if (file != null) {
                String filename = file.getOriginalFilename();
                realPath = session.getServletContext().getRealPath("/import/");
                String filepath = realPath + File.separator + filename;
                new File(filepath).mkdirs();
                try {
                    file.transferTo(new File(filepath));
                } catch (IOException e) {
                    logger.error(e);
                    throw new IOException();
                }
                //count=count+ poiUtil.importData(filepath, sourceCaseId, identifier,brotherIdentifier);
                //-------------------------------------------

                Workbook wb = null;
                Sheet sheet;
                FileInputStream in = null;
                boolean isExcel2007 = false;
                boolean isExcel2003 = false;
                try {
                    in = new FileInputStream(new File(filepath));
                    if (filepath.endsWith(".xlsx")) {
                        isExcel2007 = true;
                    } else if (filepath.endsWith(".xls")) {
                        isExcel2003 = true;
                    } else {
                        logger.error(RetStatusEnum.FILE_TYPE_EXCEPTION.getRetMsg());
                        throw new IOException();
                    }
                    if (isExcel2007) {
                        wb = new XSSFWorkbook(in);
                    } else if (isExcel2003) {
                        wb = new HSSFWorkbook(in);
                    }
                } catch (FileNotFoundException e) {
                    logger.error(RetStatusEnum.FILE_NOT_FIND.getRetMsg(), e);
                    throw new FileNotFoundException();


                }catch (IOException e) {
                    logger.error(RetStatusEnum.FILE_IO_EXCEPTION.getRetMsg(), e);
                    throw new IOException();

                }finally {
                    if(in!=null){
                        try {
                            in.close();
                        } catch (IOException e) {
                            logger.error(e);
                            throw new IOException();
                        }
                    }
                }
                sheet = wb.getSheetAt(0);
                rowCount = sheet.getLastRowNum() + 1;
                String pointValue;
                int pointCount;
                int prepointCount;
                String fatherIdentifier;
                String subIdentifier;
                String nextValue = "";
                int pointNextCount;
                String caseId = "";
                List list;
                Row row;
                int i = 0;
                String targetId="";

                for (i = 1; i < rowCount; i++) {
                    row = sheet.getRow(i);
                    //先将带id的更新
                    if(row.getCell(11)!=null){
                        targetId = row.getCell(11).getStringCellValue();
                    }

                    if (!FrameworkHelper.isEmpty(targetId)) {
                  
                            //更新操作

                            
                    }else{
                      
                           //插入操作
                            
                        }
                      
                      
                    }
                }


            }
        }


        return rowCount;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值