文章目录

问题

SpringBoot项目中使用Druid连接池,遇到下面的错误:
Cause: java.sql.SQLException: connection holder is null
; uncategorized SQLException; SQL state [null]; error code [0]; connection holder is null; nested exception is java.sql.SQLException: connection holder is null

异常的出现是属于获取连接超时,从而找不到持有者。
上面的错误信息,是来自于Druid源码,大家有兴趣可以看一下。

网上解决方法比较多的都是修改配置文件,设置abandoned超时时间,我的项目中的配置如下,看起来是没有问题的:

spring:
  datasource:
  	...
    removeAbandoned: true # 是否自动回收超时连接
    removeAbandonedTimeout: 1800 # 超时时间(以秒数为单位)

分析和解决

分析下来,原因应该是程序中有地方连接未关闭造成的。
那如何来定呢?使用druid连接池的超时回收机制,在配置中增加以下内容:

spring:
  datasource:
  	...
    removeAbandoned: true # 是否自动回收超时连接
    removeAbandonedTimeout: 1800 # 超时时间(以秒数为单位)
    logAbandoned: true # 关闭abandoned连接时输出错误日志

运行程序,当Druid强制进行回收时,会输出异常日志。
很清楚地看到是在哪里打开的连接未关闭一直在占有。
此配置项会影响性能,只在排查的时候打开。
系统运行时最好关闭。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐