天天看点

关于spring boot整合druid连接池在程序关闭后连接不释放问题解决

最近公司使用spring boot-druid-dubbo的模式开发业务,但是在线下进行实际测试时,发现mysql数据库连接数在一段时间后会被占满,顿时一脸懵逼.

经过测试,连接不释放问题是由于在自己本机对程序进行强制关闭导致的(之前做业务时,直接关闭tomcat,会自动释放与数据库的连接),强制关闭,数据库侧是不知道你程序已经关闭的,所以连接会一直保持,直到到达数据库默认超时时间后连接自动释放。

Spring boot本身是存在shutdown的,经过测试其shutdown方法,关闭程序后数据库连接可以释放。

  shutdown配置方法如下:

1)      在pom.xml中增加如下配置:

   <!--spring boot 应用关闭的前提配置 -->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>      

2)      Application.properties中增加:

##spring boot应用关闭配置项,关闭命令(必须为post请求):curl -X POST host:port/shutdown
#启用shutdown
endpoints.shutdown.enabled=true
#禁用密码验证
endpoints.shutdown.sensitive=false      

3)      关闭命令:

curl -X POST host:port/shutdown      

必须为post方式去调用!!!

使用该方式进行spring boot的关闭,未出现连接未释放问题。

继续阅读