From 454fcb99336e50bc13c545c1d63e3a8fcb291e07 Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Thu, 2 Dec 2021 15:16:32 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=95=B0=E6=8D=AE=E5=BA=93=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E6=B1=A0):=20=E4=BF=AE=E5=A4=8D=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E8=BF=9E=E6=8E=A5=E6=B1=A0=E5=9C=A8=E5=A4=A7=E5=8E=8B?= =?UTF-8?q?=E5=8A=9B=E4=B8=8B=E5=AE=B9=E6=98=93=E5=87=BA=E7=8E=B0=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E4=B8=8D=E5=88=B0=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复数据库连接池在大压力下容易出现获取不到的问题 --- .../api/service/ApiAutomationService.java | 23 ++- .../api/service/ApiDefinitionService.java | 4 + .../ApiEnvironmentRunningParamService.java | 13 +- .../api/service/ApiModuleService.java | 7 + .../api/service/ApiScenarioModuleService.java | 7 + .../api/service/ApiScenarioReportService.java | 157 +++++++++--------- .../api/service/ApiTestCaseService.java | 16 +- .../service/HistoricalDataUpgradeService.java | 4 + .../commons/utils/ServiceUtils.java | 4 + .../commons/utils/TestPlanUtils.java | 2 - .../service/PerformanceTestService.java | 5 + .../service/CustomFieldTemplateService.java | 5 +- .../io/metersphere/service/GroupService.java | 10 ++ .../service/RelationshipEdgeService.java | 4 + .../io/metersphere/service/UserService.java | 13 ++ .../track/service/TestCaseNodeService.java | 10 ++ .../service/TestCaseReviewLoadService.java | 6 +- .../track/service/TestCaseReviewService.java | 62 +------ .../track/service/TestCaseService.java | 13 ++ .../track/service/TestPlanApiCaseService.java | 14 +- .../service/TestPlanLoadCaseService.java | 4 + .../service/TestPlanScenarioCaseService.java | 4 + .../track/service/TestPlanService.java | 9 + 23 files changed, 247 insertions(+), 149 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java index 2b92853c60..f093a8a167 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java @@ -68,6 +68,7 @@ import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.ListedHashTree; +import org.mybatis.spring.SqlSessionUtils; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -1468,8 +1469,12 @@ public class ApiAutomationService { report.setStatus(APITestStatus.Waiting.name()); batchMapper.insert(report); } - sqlSession.commit(); + sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } } + // 开始串行执行 Thread thread = new Thread(new Runnable() { @Override @@ -1572,8 +1577,10 @@ public class ApiAutomationService { batchMapper.insert(report); MessageCache.scenarioExecResourceLock.put(reportId, report); } - sqlSession.commit(); - sqlSession.clearCache(); + sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } } }); thread.start(); @@ -1595,7 +1602,6 @@ public class ApiAutomationService { TriggerMode.BATCH.name().equals(request.getTriggerMode()) ? TriggerMode.BATCH.name() : request.getReportId(), request.getRunMode()); } } - executeQueue.clear(); } /** @@ -1705,6 +1711,9 @@ public class ApiAutomationService { } testPlan.toHashTree(jmeterHashTree, testPlan.getHashTree(), new ParameterConfig()); sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } } catch (Exception ex) { MSException.throwException(ex.getMessage()); } @@ -1979,6 +1988,9 @@ public class ApiAutomationService { } } sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } return "success"; } @@ -2318,6 +2330,9 @@ public class ApiAutomationService { } } sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } } private Long getImportNextOrder(String projectId) { diff --git a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java index bc34ce8c34..378a72447e 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java @@ -59,6 +59,7 @@ import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.jorphan.collections.HashTree; +import org.mybatis.spring.SqlSessionUtils; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -1145,6 +1146,9 @@ public class ApiDefinitionService { if (!CollectionUtils.isEmpty(apiImport.getCases())) { importMsCase(apiImport, sqlSession, request); } + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } } diff --git a/backend/src/main/java/io/metersphere/api/service/ApiEnvironmentRunningParamService.java b/backend/src/main/java/io/metersphere/api/service/ApiEnvironmentRunningParamService.java index 19203e5640..069f3cc184 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiEnvironmentRunningParamService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiEnvironmentRunningParamService.java @@ -9,6 +9,7 @@ import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; +import org.mybatis.spring.SqlSessionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; @@ -102,11 +103,19 @@ public class ApiEnvironmentRunningParamService { sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE); ApiTestEnvironmentMapper batchMapper = sqlSession.getMapper(ApiTestEnvironmentMapper.class); batchMapper.updateByPrimaryKeyWithBLOBs(apiTestEnvironmentWithBLOBs); - sqlSession.commit(); + sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } }catch (Exception e){ sqlSession.rollback(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } }finally { - sqlSession.close(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } } } diff --git a/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java b/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java index 8cb6f30304..dac378693b 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiModuleService.java @@ -25,6 +25,7 @@ import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; +import org.mybatis.spring.SqlSessionUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -353,6 +354,9 @@ public class ApiModuleService extends NodeTreeService { apiDefinitionMapper.updateByPrimaryKeySelective(value); }); sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } } @Override @@ -432,6 +436,9 @@ public class ApiModuleService extends NodeTreeService { apiModuleMapper.updateByPrimaryKeySelective(value); }); sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } } public ApiModule getModuleByName(String projectId, String protocol) { diff --git a/backend/src/main/java/io/metersphere/api/service/ApiScenarioModuleService.java b/backend/src/main/java/io/metersphere/api/service/ApiScenarioModuleService.java index 2739aa8ad1..4fdf026c46 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiScenarioModuleService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiScenarioModuleService.java @@ -27,6 +27,7 @@ import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; +import org.mybatis.spring.SqlSessionUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; @@ -289,6 +290,9 @@ public class ApiScenarioModuleService extends NodeTreeService ids) { diff --git a/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java b/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java index 9be91ccb78..226f9f19bd 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiScenarioReportService.java @@ -43,6 +43,7 @@ import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; +import org.mybatis.spring.SqlSessionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.ResponseEntity; @@ -159,6 +160,7 @@ public class ApiScenarioReportService { if (report == null) { LogUtil.info("从缓存中获取场景报告:【" + test.getName() + "】"); report = MessageCache.scenarioExecResourceLock.get(test.getName()); + LogUtil.info("从缓存中获取场景报告:【" + test.getName() + "】是否为空:" + (report == null)); } if (report != null) { report.setName(report.getScenarioName() + "-" + DateUtils.getTimeStr(System.currentTimeMillis())); @@ -322,88 +324,86 @@ public class ApiScenarioReportService { ApiScenarioReport report = editReport(scenarioResult, startTime); - TestResult newResult = createTestResult(result.getTestId(), scenarioResult); - newResult.setConsole(result.getConsole()); - scenarioResult.setName(report.getScenarioName()); - newResult.addScenario(scenarioResult); - /** - * 测试计划的定时任务场景执行时,主键是提前生成的【测试报告ID】。也就是TestResult.id是【测试报告ID】。 - * report.getScenarioId中存放的是 TestPlanApiScenario.id:TestPlanReport.id 由于参数限制,只得将两个ID拼接起来 - * 拆分report.getScenarioId, 查出ScenarioId,将真正的场景ID赋值回去 - * 同时将testPlanReportID存入集合,逻辑走完后更新TestPlanReport - */ - String[] idArr = report.getScenarioId().split(":"); - String planScenarioId = null; - if (idArr.length > 1) { - planScenarioId = idArr[0]; - String planReportID = idArr[1]; - if (!testPlanReportIdList.contains(planReportID)) { - testPlanReportIdList.add(planReportID); - } - } else { - planScenarioId = report.getScenarioId(); - } - TestPlanApiScenario testPlanApiScenario = testPlanApiScenarioMapper.selectByPrimaryKey(planScenarioId); - report.setScenarioId(testPlanApiScenario.getApiScenarioId()); - report.setTestPlanScenarioId(planScenarioId); - report.setEndTime(System.currentTimeMillis()); - apiScenarioReportMapper.updateByPrimaryKeySelective(report); - planScenarioReportMap.put(planScenarioId, report.getId()); + if(report != null){ + TestResult newResult = createTestResult(result.getTestId(), scenarioResult); + newResult.setConsole(result.getConsole()); + scenarioResult.setName(report.getScenarioName()); - - if (scenarioResult.getError() > 0) { - scenarioAndErrorMap.put(testPlanApiScenario.getId(), TestPlanApiExecuteStatus.FAILD.name()); - testPlanApiScenario.setLastResult(ScenarioStatus.Fail.name()); - } else { - scenarioAndErrorMap.put(testPlanApiScenario.getId(), TestPlanApiExecuteStatus.SUCCESS.name()); - testPlanApiScenario.setLastResult(ScenarioStatus.Success.name()); - } - - String passRate = new DecimalFormat("0%").format((float) scenarioResult.getSuccess() / (scenarioResult.getSuccess() + scenarioResult.getError())); - testPlanApiScenario.setPassRate(passRate); - // 报告详情内容 - ApiScenarioReportDetail detail = new ApiScenarioReportDetail(); - - detail.setContent(JSON.toJSONString(newResult).getBytes(StandardCharsets.UTF_8)); - detail.setReportId(report.getId()); - detail.setProjectId(report.getProjectId()); - if (StringUtils.isNotEmpty(report.getTriggerMode()) && report.getTriggerMode().equals("CASE")) { - report.setTriggerMode(TriggerMode.MANUAL.name()); - } - apiScenarioReportDetailMapper.insert(detail); - - testPlanApiScenario.setReportId(report.getId()); - report.setEndTime(System.currentTimeMillis()); - testPlanApiScenario.setUpdateTime(System.currentTimeMillis()); - testPlanApiScenarioMapper.updateByPrimaryKeySelective(testPlanApiScenario); - scenarioIdList.add(testPlanApiScenario.getApiScenarioId()); - scenarioNames.append(report.getName()).append(","); - - // 更新场景状态 - ApiScenario scenario = apiScenarioMapper.selectByPrimaryKey(testPlanApiScenario.getApiScenarioId()); - if (scenario != null) { - if (scenarioResult.getError() > 0) { - scenario.setLastResult("Fail"); + newResult.addScenario(scenarioResult); + /** + * 测试计划的定时任务场景执行时,主键是提前生成的【测试报告ID】。也就是TestResult.id是【测试报告ID】。 + * report.getScenarioId中存放的是 TestPlanApiScenario.id:TestPlanReport.id 由于参数限制,只得将两个ID拼接起来 + * 拆分report.getScenarioId, 查出ScenarioId,将真正的场景ID赋值回去 + * 同时将testPlanReportID存入集合,逻辑走完后更新TestPlanReport + */ + String[] idArr = report.getScenarioId().split(":"); + String planScenarioId = null; + if (idArr.length > 1) { + planScenarioId = idArr[0]; + String planReportID = idArr[1]; + if (!testPlanReportIdList.contains(planReportID)) { + testPlanReportIdList.add(planReportID); + } } else { - scenario.setLastResult("Success"); + planScenarioId = report.getScenarioId(); } - scenario.setPassRate(passRate); - scenario.setReportId(report.getId()); - int executeTimes = 0; - if (scenario.getExecuteTimes() != null) { - executeTimes = scenario.getExecuteTimes().intValue(); - } - scenario.setExecuteTimes(executeTimes + 1); + TestPlanApiScenario testPlanApiScenario = testPlanApiScenarioMapper.selectByPrimaryKey(planScenarioId); + report.setScenarioId(testPlanApiScenario.getApiScenarioId()); + report.setTestPlanScenarioId(planScenarioId); + report.setEndTime(System.currentTimeMillis()); + apiScenarioReportMapper.updateByPrimaryKeySelective(report); + planScenarioReportMap.put(planScenarioId, report.getId()); - apiScenarioMapper.updateByPrimaryKey(scenario); - // 发送通知 -// sendNotice(scenario); + if (scenarioResult.getError() > 0) { + scenarioAndErrorMap.put(testPlanApiScenario.getId(), TestPlanApiExecuteStatus.FAILD.name()); + testPlanApiScenario.setLastResult(ScenarioStatus.Fail.name()); + } else { + scenarioAndErrorMap.put(testPlanApiScenario.getId(), TestPlanApiExecuteStatus.SUCCESS.name()); + testPlanApiScenario.setLastResult(ScenarioStatus.Success.name()); + } + + String passRate = new DecimalFormat("0%").format((float) scenarioResult.getSuccess() / (scenarioResult.getSuccess() + scenarioResult.getError())); + testPlanApiScenario.setPassRate(passRate); + // 报告详情内容 + ApiScenarioReportDetail detail = new ApiScenarioReportDetail(); + + detail.setContent(JSON.toJSONString(newResult).getBytes(StandardCharsets.UTF_8)); + detail.setReportId(report.getId()); + detail.setProjectId(report.getProjectId()); + if (StringUtils.isNotEmpty(report.getTriggerMode()) && report.getTriggerMode().equals("CASE")) { + report.setTriggerMode(TriggerMode.MANUAL.name()); + } + apiScenarioReportDetailMapper.insert(detail); + + testPlanApiScenario.setReportId(report.getId()); + report.setEndTime(System.currentTimeMillis()); + testPlanApiScenario.setUpdateTime(System.currentTimeMillis()); + testPlanApiScenarioMapper.updateByPrimaryKeySelective(testPlanApiScenario); + scenarioIdList.add(testPlanApiScenario.getApiScenarioId()); + scenarioNames.append(report.getName()).append(","); + + // 更新场景状态 + ApiScenario scenario = apiScenarioMapper.selectByPrimaryKey(testPlanApiScenario.getApiScenarioId()); + if (scenario != null) { + if (scenarioResult.getError() > 0) { + scenario.setLastResult("Fail"); + } else { + scenario.setLastResult("Success"); + } + scenario.setPassRate(passRate); + scenario.setReportId(report.getId()); + int executeTimes = 0; + if (scenario.getExecuteTimes() != null) { + executeTimes = scenario.getExecuteTimes().intValue(); + } + scenario.setExecuteTimes(executeTimes + 1); + + apiScenarioMapper.updateByPrimaryKey(scenario); + } + lastReport = report; + MessageCache.executionQueue.remove(report.getId()); + reportIds.add(report.getId()); } - - lastReport = report; - - MessageCache.executionQueue.remove(report.getId()); - reportIds.add(report.getId()); } testPlanLog.info("TestPlanReportId" + JSONArray.toJSONString(testPlanReportIdList) + " EXECUTE OVER. SCENARIO STATUS : " + JSONObject.toJSONString(scenarioAndErrorMap)); for (String reportId : testPlanReportIdList) { @@ -505,6 +505,9 @@ public class ApiScenarioReportService { scenarioReportMapper.updateByPrimaryKeySelective(scenario); }); sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } } passRateMap.clear(); } diff --git a/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java b/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java index da60991019..ec9d2a642c 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java @@ -46,6 +46,7 @@ import org.apache.ibatis.session.SqlSessionFactory; import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.ListedHashTree; import org.aspectj.util.FileUtil; +import org.mybatis.spring.SqlSessionUtils; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -593,6 +594,9 @@ public class ApiTestCaseService { testCaseReviewMapper.updateByPrimaryKey(testCaseReview); } sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } } public List selectIdsNotExistsInPlan(String projectId, String planId) { @@ -676,6 +680,9 @@ public class ApiTestCaseService { batchMapper.updateByPrimaryKeySelective(apiTestCase); }); sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } } } @@ -714,6 +721,9 @@ public class ApiTestCaseService { batchMapper.updateByPrimaryKeySelective(apiTestCase); }); sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } } } @@ -788,8 +798,10 @@ public class ApiTestCaseService { batchMapper.insert(report); executeQueue.add(runCaseRequest); } - sqlSession.commit(); - + sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } for (RunCaseRequest runCaseRequest : executeQueue) { MessageCache.caseExecResourceLock.put(runCaseRequest.getReportId(), runCaseRequest.getReport()); run(runCaseRequest); diff --git a/backend/src/main/java/io/metersphere/api/service/HistoricalDataUpgradeService.java b/backend/src/main/java/io/metersphere/api/service/HistoricalDataUpgradeService.java index 73ba8150e6..de62122d07 100644 --- a/backend/src/main/java/io/metersphere/api/service/HistoricalDataUpgradeService.java +++ b/backend/src/main/java/io/metersphere/api/service/HistoricalDataUpgradeService.java @@ -38,6 +38,7 @@ import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; +import org.mybatis.spring.SqlSessionUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -448,6 +449,9 @@ public class HistoricalDataUpgradeService { createApiScenarioWithBLOBs(saveHistoricalDataUpgrade, scenarioTest.getId(), scenarioTest.getName(), listSteps.size(), scenarioDefinition, mapper, num); } sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } return null; } diff --git a/backend/src/main/java/io/metersphere/commons/utils/ServiceUtils.java b/backend/src/main/java/io/metersphere/commons/utils/ServiceUtils.java index 7c04b0ad8c..8375dfa882 100644 --- a/backend/src/main/java/io/metersphere/commons/utils/ServiceUtils.java +++ b/backend/src/main/java/io/metersphere/commons/utils/ServiceUtils.java @@ -13,6 +13,7 @@ import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; +import org.mybatis.spring.SqlSessionUtils; import java.lang.reflect.Method; import java.util.ArrayList; @@ -159,6 +160,9 @@ public class ServiceUtils { } sqlSession.flushStatements(); } + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } } catch (Throwable e) { LogUtil.error(e.getMessage(), e); MSException.throwException("初始化 order 字段失败"); diff --git a/backend/src/main/java/io/metersphere/commons/utils/TestPlanUtils.java b/backend/src/main/java/io/metersphere/commons/utils/TestPlanUtils.java index 12f27dd520..a45fd55ede 100644 --- a/backend/src/main/java/io/metersphere/commons/utils/TestPlanUtils.java +++ b/backend/src/main/java/io/metersphere/commons/utils/TestPlanUtils.java @@ -59,8 +59,6 @@ public class TestPlanUtils { if (StringUtils.equals(successStatus, status)) { report.setPassCount(report.getPassCount() + 1); } - }else { - System.out.println(status); } TestPlanUtils.getStatusResultMap(statusResultMap, status); }); diff --git a/backend/src/main/java/io/metersphere/performance/service/PerformanceTestService.java b/backend/src/main/java/io/metersphere/performance/service/PerformanceTestService.java index 638d0513a7..b85100e613 100644 --- a/backend/src/main/java/io/metersphere/performance/service/PerformanceTestService.java +++ b/backend/src/main/java/io/metersphere/performance/service/PerformanceTestService.java @@ -51,6 +51,7 @@ import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.aspectj.util.FileUtil; +import org.mybatis.spring.SqlSessionUtils; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -852,6 +853,10 @@ public class PerformanceTestService { scenarioLoadTest.setId(UUID.randomUUID().toString()); mapper.insert(scenarioLoadTest); }); + sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } } public Integer getGranularity(String reportId) { diff --git a/backend/src/main/java/io/metersphere/service/CustomFieldTemplateService.java b/backend/src/main/java/io/metersphere/service/CustomFieldTemplateService.java index 1e4ab9d5f7..e0c75a13d6 100644 --- a/backend/src/main/java/io/metersphere/service/CustomFieldTemplateService.java +++ b/backend/src/main/java/io/metersphere/service/CustomFieldTemplateService.java @@ -12,6 +12,7 @@ import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; +import org.mybatis.spring.SqlSessionUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -111,7 +112,9 @@ public class CustomFieldTemplateService { }); } sqlSession.flushStatements(); - sqlSession.close(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } } diff --git a/backend/src/main/java/io/metersphere/service/GroupService.java b/backend/src/main/java/io/metersphere/service/GroupService.java index c7ccb69a90..8d7afcf3f0 100644 --- a/backend/src/main/java/io/metersphere/service/GroupService.java +++ b/backend/src/main/java/io/metersphere/service/GroupService.java @@ -28,6 +28,7 @@ import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.poi.ss.formula.functions.T; +import org.mybatis.spring.SqlSessionUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -199,6 +200,9 @@ public class GroupService { } }); sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } } public List getGroupByType(EditGroupRequest request) { @@ -438,6 +442,9 @@ public class GroupService { mapper.insertSelective(userGroup); } sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } } } @@ -467,6 +474,9 @@ public class GroupService { mapper.insertSelective(userGroup); } sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } } } } diff --git a/backend/src/main/java/io/metersphere/service/RelationshipEdgeService.java b/backend/src/main/java/io/metersphere/service/RelationshipEdgeService.java index 616b18b86b..90896060f2 100644 --- a/backend/src/main/java/io/metersphere/service/RelationshipEdgeService.java +++ b/backend/src/main/java/io/metersphere/service/RelationshipEdgeService.java @@ -14,6 +14,7 @@ import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; +import org.mybatis.spring.SqlSessionUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -249,6 +250,9 @@ public class RelationshipEdgeService { } }); sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } } private RelationshipEdge getNewRelationshipEdge(String graphId, String sourceId, String targetId, String type) { diff --git a/backend/src/main/java/io/metersphere/service/UserService.java b/backend/src/main/java/io/metersphere/service/UserService.java index 5a16470da0..7473fbe825 100644 --- a/backend/src/main/java/io/metersphere/service/UserService.java +++ b/backend/src/main/java/io/metersphere/service/UserService.java @@ -48,6 +48,7 @@ import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.*; import org.apache.shiro.authz.UnauthorizedException; import org.apache.shiro.subject.Subject; +import org.mybatis.spring.SqlSessionUtils; import org.springframework.beans.BeanUtils; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; @@ -154,6 +155,9 @@ public class UserService { mapper.insertSelective(userGroup); } sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } } } @@ -856,6 +860,9 @@ public class UserService { mapper.insertSelective(userGroup); } sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } } } } @@ -903,6 +910,9 @@ public class UserService { mapper.insertSelective(userGroup); } sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } } } @@ -1226,6 +1236,9 @@ public class UserService { mapper.insertSelective(userGroup); } sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } } } } diff --git a/backend/src/main/java/io/metersphere/track/service/TestCaseNodeService.java b/backend/src/main/java/io/metersphere/track/service/TestCaseNodeService.java index ba4f362205..0267104715 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestCaseNodeService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestCaseNodeService.java @@ -30,6 +30,7 @@ import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; +import org.mybatis.spring.SqlSessionUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; @@ -568,6 +569,9 @@ public class TestCaseNodeService extends NodeTreeService { testCaseNodeMapper.updateByPrimaryKeySelective(value); }); sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } } private void batchUpdateTestCase(List testCases) { @@ -577,6 +581,9 @@ public class TestCaseNodeService extends NodeTreeService { testCaseMapper.updateByPrimaryKeySelective(value); }); sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } } private List QueryTestCaseByNodeIds(List nodeIds) { @@ -678,6 +685,9 @@ public class TestCaseNodeService extends NodeTreeService { testCaseNodeMapper.updateByPrimaryKey(node); }); sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } } } diff --git a/backend/src/main/java/io/metersphere/track/service/TestCaseReviewLoadService.java b/backend/src/main/java/io/metersphere/track/service/TestCaseReviewLoadService.java index c83f343964..afec6802c3 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestCaseReviewLoadService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestCaseReviewLoadService.java @@ -16,6 +16,7 @@ import io.metersphere.track.request.testreview.TestReviewRequest; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; +import org.mybatis.spring.SqlSessionUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; @@ -88,6 +89,9 @@ public class TestCaseReviewLoadService { testCaseReviewMapper.updateByPrimaryKey(testCaseReview); } sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } } public void delete(String id) { @@ -104,7 +108,7 @@ public class TestCaseReviewLoadService { testCaseReviewLoadMapper.updateByPrimaryKeySelective(testCaseReviewLoad); return reportId; } -//??? + public Boolean isExistReport(LoadCaseReportRequest request) { String reportId = request.getReportId(); String testPlanLoadCaseId = request.getTestPlanLoadCaseId(); diff --git a/backend/src/main/java/io/metersphere/track/service/TestCaseReviewService.java b/backend/src/main/java/io/metersphere/track/service/TestCaseReviewService.java index ec17fb1cf8..fd7090f3e4 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestCaseReviewService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestCaseReviewService.java @@ -36,6 +36,7 @@ import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; +import org.mybatis.spring.SqlSessionUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -389,65 +390,10 @@ public class TestCaseReviewService { } sqlSession.flushStatements(); - //同步添加关联的接口和测试用例 - /* if(request.getChecked()){ - if (!testCaseIds.isEmpty()) { - testCaseIds.forEach(caseId -> { - TestCaseWithBLOBs testDtail=testCaseMapper.selectByPrimaryKey(caseId); - if(StringUtils.equals(testDtail.getType(), TestCaseStatus.performance.name())){ - TestCaseReviewLoad t=new TestCaseReviewLoad(); - t.setId(UUID.randomUUID().toString()); - t.setTestCaseReviewId(request.getReviewId()); - t.setLoadCaseId(testDtail.getTestId()); - t.setCreateTime(System.currentTimeMillis()); - t.setUpdateTime(System.currentTimeMillis()); - TestCaseReviewLoadExample example=new TestCaseReviewLoadExample(); - example.createCriteria().andTestCaseReviewIdEqualTo(request.getReviewId()).andLoadCaseIdEqualTo(t.getLoadCaseId()); - if (testCaseReviewLoadMapper.countByExample(example) <=0) { - testCaseReviewLoadMapper.insert(t); - } + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } - } - if(StringUtils.equals(testDtail.getType(),TestCaseStatus.testcase.name())){ - TestCaseReviewApiCase t=new TestCaseReviewApiCase(); - ApiTestCaseWithBLOBs apitest=apiTestCaseMapper.selectByPrimaryKey(testDtail.getTestId()); - ApiDefinitionWithBLOBs apidefinition=apiDefinitionMapper.selectByPrimaryKey(apitest.getApiDefinitionId()); - t.setId(UUID.randomUUID().toString()); - t.setTestCaseReviewId(request.getReviewId()); - t.setApiCaseId(testDtail.getTestId()); - t.setEnvironmentId(apidefinition.getEnvironmentId()); - t.setCreateTime(System.currentTimeMillis()); - t.setUpdateTime(System.currentTimeMillis()); - TestCaseReviewApiCaseExample example=new TestCaseReviewApiCaseExample(); - example.createCriteria().andTestCaseReviewIdEqualTo(request.getReviewId()).andApiCaseIdEqualTo(t.getApiCaseId()); - if(testCaseReviewApiCaseMapper.countByExample(example)<=0){ - testCaseReviewApiCaseMapper.insert(t); - } - - } - if(StringUtils.equals(testDtail.getType(),TestCaseStatus.automation.name())){ - TestCaseReviewScenario t=new TestCaseReviewScenario(); - ApiScenarioWithBLOBs testPlanApiScenario=apiScenarioMapper.selectByPrimaryKey(testDtail.getTestId()); - t.setId(UUID.randomUUID().toString()); - t.setTestCaseReviewId(request.getReviewId()); - t.setApiScenarioId(testDtail.getTestId()); - t.setLastResult(testPlanApiScenario.getLastResult()); - t.setPassRate(testPlanApiScenario.getPassRate()); - t.setReportId(testPlanApiScenario.getReportId()); - t.setStatus(testPlanApiScenario.getStatus()); - t.setCreateTime(System.currentTimeMillis()); - t.setUpdateTime(System.currentTimeMillis()); - TestCaseReviewScenarioExample example=new TestCaseReviewScenarioExample(); - example.createCriteria().andTestCaseReviewIdEqualTo(request.getReviewId()).andApiScenarioIdEqualTo(t.getApiScenarioId()); - if(testCaseReviewScenarioMapper.countByExample(example)<=0){ - testCaseReviewScenarioMapper.insert(t); - } - - } - - }); - } - }*/ TestCaseReview testCaseReview = testCaseReviewMapper.selectByPrimaryKey(request.getReviewId()); if (StringUtils.equals(testCaseReview.getStatus(), TestCaseReviewStatus.Prepare.name()) || StringUtils.equals(testCaseReview.getStatus(), TestCaseReviewStatus.Completed.name())) { diff --git a/backend/src/main/java/io/metersphere/track/service/TestCaseService.java b/backend/src/main/java/io/metersphere/track/service/TestCaseService.java index 155ed5905d..6705aca5f1 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestCaseService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestCaseService.java @@ -58,6 +58,7 @@ import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; +import org.mybatis.spring.SqlSessionUtils; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -674,6 +675,9 @@ public class TestCaseService { } } sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } } public void updateImportData(List testCases, String projectId) { @@ -699,6 +703,9 @@ public class TestCaseService { }); } sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } } private String updateCustomField(String customFields, String priority) { @@ -756,6 +763,9 @@ public class TestCaseService { }); } sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } } /** @@ -795,6 +805,9 @@ public class TestCaseService { }); } sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } } public void testCaseTemplateExport(String projectId, String importType, HttpServletResponse response) { diff --git a/backend/src/main/java/io/metersphere/track/service/TestPlanApiCaseService.java b/backend/src/main/java/io/metersphere/track/service/TestPlanApiCaseService.java index c09b2dc5fd..9a241ced81 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestPlanApiCaseService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestPlanApiCaseService.java @@ -56,6 +56,7 @@ import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.jorphan.collections.HashTree; +import org.mybatis.spring.SqlSessionUtils; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -459,8 +460,11 @@ public class TestPlanApiCaseService { TestPlanReportExecuteCatch.updateTestPlanThreadInfo(request.getPlanReportId(), executeThreadIdMap, null, null); } } + sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } - sqlSession.commit(); List reportIds = new LinkedList<>(); // 开始串行执行 Thread thread = new Thread(new Runnable() { @@ -562,7 +566,10 @@ public class TestPlanApiCaseService { executeThreadIdMap.put(testPlanApiCase.getId(), report.getId()); MessageCache.caseExecResourceLock.put(report.getId(), report); }); - sqlSession.commit(); + sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } //如果是测试计划生成报告的执行,则更新执行信息、执行线程信息。 if (TestPlanReportExecuteCatch.containsReport(request.getPlanReportId())) { if (!executeThreadIdMap.isEmpty()) { @@ -677,6 +684,9 @@ public class TestPlanApiCaseService { }); sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } } public Boolean hasFailCase(String planId, List apiCaseIds) { diff --git a/backend/src/main/java/io/metersphere/track/service/TestPlanLoadCaseService.java b/backend/src/main/java/io/metersphere/track/service/TestPlanLoadCaseService.java index c8b94cf828..15e28b101e 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestPlanLoadCaseService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestPlanLoadCaseService.java @@ -31,6 +31,7 @@ import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; +import org.mybatis.spring.SqlSessionUtils; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -128,6 +129,9 @@ public class TestPlanLoadCaseService { testPlanMapper.updateByPrimaryKey(testPlan); } sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } } public void delete(String id) { diff --git a/backend/src/main/java/io/metersphere/track/service/TestPlanScenarioCaseService.java b/backend/src/main/java/io/metersphere/track/service/TestPlanScenarioCaseService.java index 0e90809af0..a214983c43 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestPlanScenarioCaseService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestPlanScenarioCaseService.java @@ -31,6 +31,7 @@ import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; +import org.mybatis.spring.SqlSessionUtils; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -247,6 +248,9 @@ public class TestPlanScenarioCaseService { mapper.updateByPrimaryKeyWithBLOBs(testPlanApiScenario); } sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } return; } diff --git a/backend/src/main/java/io/metersphere/track/service/TestPlanService.java b/backend/src/main/java/io/metersphere/track/service/TestPlanService.java index 457055a691..e15d641499 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestPlanService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestPlanService.java @@ -66,6 +66,7 @@ import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.ListedHashTree; +import org.mybatis.spring.SqlSessionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Lazy; @@ -668,6 +669,10 @@ public class TestPlanService { testPlan.setActualEndTime(null); testPlanMapper.updateByPrimaryKey(testPlan); } + sqlSession.flushStatements(); + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } } public List recentTestPlans(String projectId) { @@ -1438,6 +1443,10 @@ public class TestPlanService { } } sqlSession.flushStatements(); + + if (sqlSession != null && sqlSessionFactory != null) { + SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); + } } }