Spring Boot 修改數(shù)據(jù)庫連接池

[ WARN ] [2016-07-11 09:52:45] org.hibernate.engine.jdbc.spi.SqlExceptionHelper [144] - SQL Error: 0, SQLState: 08S01
[ ERROR] [2016-07-11 09:52:45] org.hibernate.engine.jdbc.spi.SqlExceptionHelper [146] - The last packet successfully received from the server was 229,451,844 milliseconds ago.  The last packet sent successfully to the server was 229,451,844 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
[ ERROR] [2016-07-11 09:52:45] org.springframework.boot.context.web.ErrorPageFilter [178] - Forwarding to error page from request [/getPeopleNearbyList] due to exception [could not extract ResultSet; nested exception is org.hibernate.exception.JDBCConnectionException: could not extract ResultSet]
org.springframework.dao.DataAccessResourceFailureException: could not extract ResultSet; nested exception is org.hibernate.exception.JDBCConnectionException: could not extract ResultSet

應(yīng)用跑了兩天,報(bào)了這樣一個(gè)錯(cuò)誤。

MySQL連接時(shí),服務(wù)器默認(rèn)的“wait_timeout”是8小時(shí),也就是說一個(gè)connection空閑超過8個(gè)小時(shí),Mysql將自動(dòng)斷開該connection。connections如果空閑超過8小時(shí),Mysql將其斷開,而DBCP并不知道該connection已經(jīng)失效,如果這時(shí)有Client請求connection,DBCP將該失效的Connection提供給Client,將會(huì)造成異常

Spring Boot中,默認(rèn)使用的連接池為JDBC Connection Pool。

那樣,在JDBC Connection Pool中我們需要處理好以下問題:
1、對每個(gè)連接進(jìn)行檢查
2、對一次操作數(shù)據(jù)庫超過多少時(shí)間的連接進(jìn)行移除
3、每隔多少時(shí)間檢測一次連接
4、一個(gè)連接在連接多少時(shí)間后,就必須刪除

#這里可以制定自己要使用的連接池
#Fully qualified name of the connection pool implementation to use. By default, it is auto-detected from the classpath.
spring.datasource.type=這里可以不用添加此項(xiàng)
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8
spring.datasource.username=數(shù)據(jù)庫用戶名
spring.datasource.password=數(shù)據(jù)庫密碼
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# 初始化大小,最小,最大
spring.datasource.initial-size=5
spring.datasource.min-idle=5
spring.datasource.max-idle=100
spring.datasource.max-wait=10000
spring.datasource.validation-query=SELECT 1
#沒次使用連接時(shí)進(jìn)行校驗(yàn),會(huì)影響系統(tǒng)性能。默認(rèn)為false
#spring.datasource.test-on-borrow=true
spring.datasource.test-while-idle=true
spring.datasource.time-between-eviction-runs-millis=27800
#spring.datasource.jdbc-interceptors=ConnectionState;SlowQueryReport(threshold=0)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容