现象;
前段时间在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. DOUAudioStreamer 中的DOUAudioFileProvider理解笔记

    概览 DOUAudioFileProvider读取音频文件local,ipod-library,remote audiofile(通过DOUSimpleHTTPRequest封装的CFHTTPMess ...

  2. SQL构架(转)

    在一次的实际工作中碰到以下情况,在 SQL SERVER 2008中,新建了一个新用户去访问几张由其他用户创建的表,但是无法进行查询,提示“对象名'CustomEntry' 无效.”.当带上了架构名称 ...

  3. WPF中使用USERCONTROL

    继续这两篇文章写: http://daniex.info/wpf-using-usercontrol.html http://www.codeproject.com/Articles/32825/Ho ...

  4. 第45篇 js操作打开本地程序

    原文地址:http://blog.laofu.online/2017/06/10/how-js-controlApp/ 背景 假设有这样一个产品,一个web和一个winform客户端,在客户在web的 ...

  5. twemproxy发送流程探索——剖析twemproxy代码正编

    本文想要完成对twemproxy发送流程--msg_send的探索,对于twemproxy发送流程的数据结构已经在<twemproxy接收流程探索--剖析twemproxy代码正编>介绍过 ...

  6. ActiveMQ 和消息简介

    Apache ActiveMQ 是远程系统间进行通信的消息代理,实现了 JMS(Java Message Service,Java 消息服务).尽管 ActiveMQ 是使用 Java 写的,但是其提 ...

  7. Webpack 代码分离

    Webpack 代码分离 代码分离是 webpack 中最引人注目的特性之一. 你可以把你的代码分离到不同的 bundle 中,然后你就可以去按需加载这些文件. 总的来说, webpack 分离可以分 ...

  8. Java基础学习之线程

    1.尽量避免直接使用 同步.Thread等原生操作线程的类,可以通过java.util.concurrent下对底层包装好的类进行操作, ReentrantLock:lock trylock  unl ...

  9. Bash环境配置文件

    一.环境配置文件读取优先级 其中~/.bash_profile,~/.bash_login,~/.profile三个文件只有一个有效,查找优先级从左至右降低.bash会一直检查是否有~/.bashrc ...

  10. C# 短信发送 邮件发送

    兴趣是最好的老师. --爱因斯坦 一.实现短信发送 1.使用短信mao的方式进行短信发送,前提要购买硬件设备,这里就不考虑展示了: 2.使用中国网建提供的短信平台,但是用几次后要收费: 我们这里主要介 ...