最近做了个复杂的功能,设计多个表多次的插入,更新操作,无奈事务不起作用:
经过查找资料,问同事 导致事务失效原因
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; }