现象;
前段时间在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包中的类了。

单元测试报connection is allready closed导致dailybuild中断的解决方案——类加载机制的应用的更多相关文章

  1. Database mirroring connection error 4 'An error occurred while receiving data: '10054(An existing connection was forcibly closed by the remote host.)

    公司一SQL Server镜像发生了故障转移(主备切换),检查SQL Server镜像发生主备切换的原因,在错误日志中发现下面错误: Date        2019/8/31 14:09:17   ...

  2. Spring事务报Connection is read-only

    昨天做项目时,写了个方法,程序突然报了Connection is readonly. Queries leading to data modification are not allowed调了程序半 ...

  3. 安装好的虚拟机,外部通过ssh工具连接,报connection failed

    今天,新装了一台ubuntu虚拟机,安装成功以后,准备利用Xshell从外部访问linux,以减少切换,但是,在连接时,总是会报:connection failed. 于是,写下这篇随笔,以增加记忆且 ...

  4. main方法或者junit单元测试报 类找不到异常

    MyEclipse10.7+Maven项目junit单元测试报找不到类异常,附正常编译后的输出设置   1 首先想到的是输出路径错误 一般不是maven工程的项目编译后的.class文件会在/weba ...

  5. 【centOS】【xshell】xshell连接虚拟机上的centOS,操作途中突然断开连接,报错:connect closed by foreign host

    如题  xshell连接虚拟机上的centOS,操作途中突然断开连接,报错:connect closed by foreign host 快捷解决方法: 在虚拟机上centOS重新启动网络,即可解决问 ...

  6. An existing connection was forcibly closed by the remote host

    StackOverflow https://stackoverflow.com/questions/5420656/unable-to-read-data-from-the-transport-con ...

  7. junit单元测试报错Failed to load ApplicationContext,但是项目发布到tomcat浏览器访问没问题

    junit单元测试报错Failed to load ApplicationContext,但是项目发布到tomcat浏览器访问没问题,说明代码是没问题的,配置也没问题.开始时怀疑是我使用junit版本 ...

  8. By default, the connection will be closed if the proxied server does not transmit any data within 60 seconds.

    WebSocket proxying https://nginx.org/en/docs/http/websocket.html By default, the connection will be ...

  9. MySQL中char(36)被认为是GUID导致的BUG及解决方案

    MySQL中char(36)被认为是GUID导致的BUG及解决方案 有时候在使用Toad或在程序中,偶尔会遇到如下的错误: System.FormatException GUID 应包含带 4 个短划 ...

随机推荐

  1. python不使用第三方变量,交换两个变量的值

    #不使用第三个变量交换两个变量的值 a=1 b=2 a,b=b,a#python的直接交换 #另一种交换方法 a=a+b#a=3 b=2 b=a-b#a=3 b=1 a=a-b#a=2 b=1 pri ...

  2. PHP 获取系统信息,PHP 获取服务器详细信息

    获取系统类型及版本号:    php_uname()                                   (例:Windows NT COMPUTER 5.1 build 2600)只 ...

  3. java小白进阶安卓第一天

  4. 一致性Hash算法与代码实现

    一致性Hash算法: 先构造一个长度为232的整数环(这个环被称为一致性Hash环),根据节点名称的Hash值(其分布为[0, 232-1])将服务器节点放置在这个Hash环上,然后根据数据的Key值 ...

  5. JQuery浮动层Loading页面加载特效

    之前做项目,经常需要一些浮动层加载Loading. 现在好多前端框架都能实现了,最常用的就是 artDialog 下面记录下当时的代码. <!DOCTYPE html PUBLIC " ...

  6. PHP面试题详解

    自己从网上找了几份常考到的PHP面试题进行了整理,然后才有了这份PHP面试题,并且我把所有的题目进行了详细分析和代码分析,希望可以对大家有帮助,谢谢大家. 这份试题我也上传到了百度云,有需要的可以直接 ...

  7. 关于MyEclipse修改项目名称后,部署到tomcat显示旧的项目名称

    问题:用Myeclipse部署项目的时候,     出现部署到tomcat下的项目是之前的项目,而不是当前的项目.   解决方案:工程名->右键->Properties->MyEcl ...

  8. MongoDB--MapReduce分布统计s

    MapReduce Mapreduce:要操作的目标集合 Map:映射函数(生成键值对序列,作为reduce函数参数) //传入分组的key和需要统计的值 Reduce:统计函数 //格式化返回的参数 ...

  9. 【RAC】RAC相关基础知识

    [RAC]RAC相关基础知识 1.CRS简介    从Oracle 10G开始,oracle引进一套完整的集群管理解决方案—-Cluster-Ready Services,它包括集群连通性.消息和锁. ...

  10. 通知栏Notification的整理

    一.介绍 通知栏适用于交互事件的通知,是位于顶层可以展开的通知列表. 二.功能作用 1.显示接收到短消息,及时消息等信息(如QQ.微信.新浪.短信)       2.显示客户端的推送消息(如有新版本发 ...