现象;
前段时间在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. Xamarin控件使用之GridView

    [Activity(Label = "MainGridViewActivity", LaunchMode = LaunchMode.SingleTop)]//设置Activity启 ...

  2. cpp(第五章)

    1.副作用,指的是在计算表达式时对某些东西(如存储在变量的值)进行修改:顺序点,是程序执行过程中的一个点,在这里,进入下一步之前将确保对所有的副作用 都进行评估.(分号就是一个顺序点).for exa ...

  3. mysql revise

    DATABASE create database db_name; use db_name; alter database db_name; drop database db_name; show d ...

  4. zookeeper 应用场景概述

    Zookeeper主要可以干哪些事情:配置管理,名字服务,提供分布式同步以及集群管理. 一 .配置管理 在我们的应用中除了代码外,还有一些就是各种配置.比如数据库连接,远程服务访问地址等.一般我们都是 ...

  5. 文件描述符与FILE

    1. 文件描述符(重点) 在Linux系统中一切皆可以看成是文件,文件又可分为:普通文件.目录文件.链接文件和设备文件.文件描述符(file descriptor)是内核为了高效管理已被打开的文件所创 ...

  6. 如何判断img标签是否有src属性

    前几天,写一个小项目,需要判断img标签是否有src属性,想了半天,只能想到用jq实现,如下: if($(".img").attr("src")==undefi ...

  7. 表连接查询的顺序和where子句条件的前后顺序会影响sql的性能么

    有好多时候,我们常听别人说大表在前,小表在后,包括现在好多百度出来的靠前的答案都有说数据库是从右到左加载的,所以from语句最后关联的那张表会先被处理.如果三表交叉,就选择交叉表来作为基础表.等等一些 ...

  8. python str转dict

    两种方法 捷径 eval(str) >>> user = "{'name' : 'jim', 'sex' : 'male', 'age': 18}" >&g ...

  9. PHP获取远程文件的几种方式

    1.fopen() 2.file_get_contents() 3.fsocket() 4.curl()

  10. ubuntu 16.04.2 源码安装gitlab并且利用runner持续集成

    参考原档:https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/install/installation.md#using-https 本章只 ...