单元测试报connection is allready closed导致dailybuild中断的解决方案——类加载机制的应用
2017-08-13 10:18
330 查看
现象;
前段时间在dailybuild过程中,经常遇到connection is allready closed错误,特别是在dailybuild高峰期.
分析定位:
这个错误是的起因比较多,这里的情况是在回滚事务时,连接已经关闭导致抛异常的,报这个错误说明用例已经执行通过,因此,这个错误是不影响覆盖率,只影响成功率的,可以不抛出这个错误。
解决方案:
新增了类/src/test/java/org/springframework/test/context/TestContextManager.java,package路径及类名跟spring中的这个类完全一致,只修改afterTestMethod方法:
再次执行5次dailybuild,问题没有再现。
原理:
利用ClassLoader的类加载机制,同一个类不会被二次加载:
在项目中,classpath的的顺序是这样的:
如上图,classloader先加载的是项目编译生成的class,因此,同一个类,如果在项目中存在的话,就不会再加载jar包中的类了。
前段时间在dailybuild过程中,经常遇到connection is allready closed错误,特别是在dailybuild高峰期.
分析定位:
这个错误是的起因比较多,这里的情况是在回滚事务时,连接已经关闭导致抛异常的,报这个错误说明用例已经执行通过,因此,这个错误是不影响覆盖率,只影响成功率的,可以不抛出这个错误。
解决方案:
新增了类/src/test/java/org/springframework/test/context/TestContextManager.java,package路径及类名跟spring中的这个类完全一致,只修改afterTestMethod方法:
public void afterTestMethod(Object testInstance, Method testMethod, Throwable exception) throws Exception { Assert.notNull(testInstance, "testInstance must not be null"); if (logger.isTraceEnabled()) { logger.trace("afterTestMethod(): instance [" + testInstance + "], method [" + testMethod + "], exception [" + exception + "]"); } getTestContext().updateState(testInstance, testMethod, exception); // Traverse the TestExecutionListeners in reverse order to ensure proper // "wrapper"-style execution of listeners. List<TestExecutionListener> listenersReversed = new ArrayList<TestExecutionListener>(getTestExecutionListeners()); Collections.reverse(listenersReversed); Exception afterTestMethodException = null; for (TestExecutionListener testExecutionListener : listenersReversed) { try { testExecutionListener.afterTestMethod(getTestContext()); } catch (Exception ex) { logger.warn("Caught exception while allowing TestExecutionListener [" + testExecutionListener + "] to process 'after' execution for test: method [" + testMethod + "], instance [" + testInstance + "], exception [" + exception + "]", ex); if (afterTestMethodException == null) { afterTestMethodException = ex; } } } if (afterTestMethodException != null) { // 捕获这种异常,不再抛出 if (afterTestMethodException.getMessage ().indexOf ("Connection has already been closed") > -1) { logger.error ("catch the exp!", afterTestMethodException); return; } throw afterTestMethodException; } }
再次执行5次dailybuild,问题没有再现。
原理:
利用ClassLoader的类加载机制,同一个类不会被二次加载:
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { synchronized (getClassLoadingLock(name)) { // First, check if the class has already been loaded Class c = findLoadedClass(name); if (c == null) {// 找不到的时候才会作为新的类加载 long t0 = System.nanoTime(); try { if (parent != null) { c = parent.loadClass(name, false); } else { c = findBootstrapClassOrNull(name); } } catch (ClassNotFoundException e) { // ClassNotFoundException thrown if class not found // from the non-null parent class loader } if (c == null) { // If still not found, then invoke findClass in order // to find the class. long t1 = System.nanoTime(); c = findClass(name); // this is the defining class loader; record the stats sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0); sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1); sun.misc.PerfCounter.getFindClasses().increment(); } } if (resolve) { resolveClass(c); } return c; } }
在项目中,classpath的的顺序是这样的:
如上图,classloader先加载的是项目编译生成的class,因此,同一个类,如果在项目中存在的话,就不会再加载jar包中的类了。
相关文章推荐
- MYSQL 连接中断次数限制 mysql:is blocked because of many connection errors解决方案
- android应用因为加入js而导致webview一直在加载中的bug解决方案
- 安卓加载大背景图片或者加载大量本地资源图片时经常导致内存溢出(Out of Memory Error)推荐几种解决方案
- Virtualbox正在运行虚拟机,被强制中断导致虚拟机无法启动(start按钮为灰色)解决方案
- 《BREW进阶与精通――3G移动增值业务的运营、定制与开发》连载之84――BREW中的应用加载机制
- c3p0数据库连接池使用报错【 You can't operate on a closed Connection!!!】解决方案
- mysql:is blocked because of many connection errors解决方案
- C#数据库操作错误:There is already an open DataReader associated with this Connection which must be closed..
- “passing argument 2 0f “request_irq” from incompatible pointer type”导致中断申请失败和模块无法加载
- ARM S3C2410X系统中断编程机制的研究与应用
- 悬而未决:atom Uncaught Error: Connection is closed.
- nginx 导致文件上传中途中断 Failed to load resource: net::ERR_CONNECTION_RESET
- Android Studio添加应用作为依赖时报错Error:Dependency MonthText:xlistview:unspecified on project app resolves to an APK archive which is not supported as a compilation dependency. File: 及其解决方案
- 应用打回, key "inter-app-audio" is not supported.解决方案
- VxWorks下MPC860的中断处理机制及其应用
- 应用直接中断连接导致数据被锁(生产故障)
- UnsatisfiedLinkError X.so is 64-bit instead of 32-bit之Android 64 bit SO加载机制
- The connection to adb is down, and a severe error has occured问题解决方案
- updatePanel的triggle导致无法加载updateProgress解决方案
- Android应用setContentView与LayoutInflater加载解析机制源码分析