最近公司使用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的關閉,未出現連接配接未釋放問題。