1:前言

最近在项目中使用多线程对大任务拆分处理时,进行数据库操作的时候报错了。

业务代码大概是这样的:

@Service
public calss TestServiceImpl implements TestService{
@Resource
private TestMapper testMapper; public void insert(Test test){
ThreadPoolExecutor executor = new ThreadPoolExecutor(...); executor.execute(() -> {
testMapper.insert(test);
});
} }

测试用例代码:

@RunWith(SpirngRunner.class)
@SpringBootTest
public class ApplicationTests{ @Resource
private TestService testService; @Test
public void insertTest(){
Test test = new Test();
testService.insert(test);
} }

2:排查思路

1:项目中使用了一个很老旧的定时器工具(LTS),由于配置未接入,启动时LTS一直会报错的,

我首先怀疑是LTS的问题,是不是内部某部分源码调用了DruidDataSource的close方法。

因此我在项目中先把LTS排除不让它启动。

结果:然并卵

2:后来怀疑是不是项目中的定时任务造成的,因为数据源关闭前打印了以下日志

可以看到 Thread-2 执行了ThreadPoolTaskExecutor 再关闭数据源的。

因此我在SpringBoot 的启动类上加上了以下代码

@SpringBootApplication(exclude = {TaskExecutionAutoConfiguration.class})

结果:然并卵

3:关电脑回家吃饭睡觉

第二天早上回到工位上缓了一会,思考还有什么原因会导致这种问题。

因为我们的线程池是一个封装的工具类封装过的(我封装的),里面有两行代码引起了我的注意:

我先把 threadPoolExecutor.awaitTermination(3, TimeUnit.MINUTES); 这行代码注释掉,重新跑测试用例。

发现并没有报错,并且程序很快就终止运行了。

在这里我就想到了,SpringBoot Test 中主线程退出,是不会管用户线程是否结束了任务的这个问题。

3:根本原因

1:SpringBoot Test 主线程退出,导致Spring 容器关闭。

2:Spring容器关闭,导致DruidDataSource 关闭

3:此时用户线程去访问已关闭的数据源,导致报错。

这样不行啊,那我测试用例怎么跑完呢?

可能这个时候有人会想到在测试用例最后面加以下代码:

TimeUnit.SECONDS.sleep(30);

可是本人觉得不够优雅,也不灵活。

4:解决方案

监听容器关闭,关闭前先判断线程池中是否存在未执行玩任务的线程。

ThreadPoolExecutor 的源码:



因此我们是可以知道是否存在未执行完任务的线程的。

所以我在项目中写了以下代码:

当线程池中存在未执行完任务的线程的时候,就先等待,直到所有任务完成或超过等待时间。

可把自己牛逼坏了(叉会腰)!

SpringBoot Test 多线程报错:dataSource already closed的更多相关文章

  1. springboot项目启动报错Failed to configure a DataSource: 'url' attribute is not specified and no embedde

    springboot项目启动报错Failed to configure a DataSource: 'url' attribute is not specified and no embedde 创建 ...

  2. Springboot数据库连接池报错的解决办法

    Springboot数据库连接池报错的解决办法 这个异常通常在Linux服务器上会发生,原因是Linux系统会主动断开一个长时间没有通信的连接 那么我们的问题就是:数据库连接池长时间处于间歇状态,导致 ...

  3. Springboot 之 启动报错-Cannot determine embedded database driver class for database type NONE

    Springboot 之 启动报错-数据库 springboot项目在启动时,报如下错误: Error starting ApplicationContext. To display the auto ...

  4. Springboot 启动文件报错,原因是@ComponentScan写成了@ComponentScans

    Springboot 启动文件报错,原因是@ComponentScan写成了@ComponentScans

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

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

  6. springboot测试启动报错java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

    springboot测试启动报错: java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you ne ...

  7. Springboot整合Elasticsearch报错availableProcessors is already set to [4], rejecting [4]

    Springboot整合Elasticsearch报错 今天使用SpringBoot整合Elasticsearch时候,相关的配置完成后,启动项目就报错了. nested exception is j ...

  8. SpringBoot集成MybatisPlus报错

    SpringBoot集成MybatisPlus报错 启动的时候总是报如下错误: java.lang.annotation.AnnotationFormatError: Invalid default: ...

  9. springboot项目启动报错 url' attribute is not specified and no embedded datasource could be configured

    报错相关信息: 2019-07-22 17:12:48.971 ERROR 8312 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : **** ...

随机推荐

  1. The Preliminary Contest for ICPC Asia Nanjing 2019 A The beautiful values of the palace(树状数组+思维)

    Here is a square matrix of n * nn∗n, each lattice has its value (nn must be odd), and the center val ...

  2. AtCoder Beginner Contest 161

    比赛链接:https://atcoder.jp/contests/abc161/tasks AtCoder Beginner Contest 161 第一次打AtCoder的比赛,因为是日本的网站终于 ...

  3. 【noi 2.6_2988】计算字符串距离(DP)

    题意: 给两个字符串,可以增.删.改,问使这两个串变为相同的最小操作数. 解法:(下面2种的代码主要区别在初始化和,而状态转移方程大家可挑自己更容易理解的方法打) 1.f[i][j]表示a串前i个和b ...

  4. hdu 1517 Multiplication Game

    题意: 用整数p乘以2到9中的一个数字.斯坦总是从p = 1开始,做乘法,然后奥利乘以这个数,然后斯坦,以此类推.游戏开始前,他们画一个整数1 < n < 4294967295,谁先到达p ...

  5. c文件二进制读取写入文件、c语言实现二进制(01)转化成txt格式文本、c读取文件名可变

    c语言实现二进制(01)转化成txt格式文本: 下面的程序只能实现ascall对应字符转换,如果文件内出现中文字符,则会出现错误. 本程序要自己创建个文本格式的输入文件a1.txt,编译后能将文本文件 ...

  6. 谈到云原生, 绕不开"容器化"

    传送门 什么是云原生? 云原生设计理念 .NET微服务 Containers 现在谈到云原生, 绕不开"容器". 在<Cloud Native Patterns>一书中 ...

  7. anaconda jupyter notebook 启动方法

    介绍 anaconda jupyter notebook是一种基于浏览器的python编译环境.(大概) 使用时可能因为浏览器缓存造成问题. 但是很方便. 启动方法 anaconda navigato ...

  8. springboot demo(一)快速开始

    快速入门 maven构建项目 1.访问http://start.spring.io/ 2.选择构建工具Maven Project.Spring Boot版本2.26以及一些工程基本信息,点击" ...

  9. HDU 3966 Aragorn's Story(树链剖分)题解

    题意:给一棵树,要求你对一个路径上的值进行加减,查询某个点的值 思路:重链剖分. 由于分了轻重儿子,我每次到重儿子的top只要O(1),经过的轻儿子最多logn条,那么我每次往上跳最多跳logn次. ...

  10. μC/OS-III---I笔记1---概述

    在裸板上跑一些程序对于一些电子设计是足够的,所谓裸板上的程序就是传统的前后台系统,而我的理解它应该是一种"过程类"的程序,一个大循环(作为后台)做一些处理,中断程序(作为前台)来处 ...