Spring Boot 與JPA 和Hibernate 的連接超時
當使用Spring Boot 與JPA-Hibernate 和MySQL 時,您可能會遇到以下錯誤:
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 56,006,037 milliseconds ago. The last packet sent successfully to the server was 56,006,037 milliseconds ago. is longer than the server configured value of 'wait_timeout'.
此問題是由於連線長時間處於不活動狀態,導致伺服器終止連線。
不建議的解決方案
一個常見的解決方案但不鼓勵的方法是在藉用時啟用連接測試:
spring.datasource.testOnBorrow=true spring.datasource.validationQuery=SELECT 1
推薦的解決方案
更全面的解決方案包括:
設定spring.datasource.url 以包含autoReconnect 屬性:
spring.datasource.url = jdbc:mysql://localhost:3306/test?autoReconnect=true
spring.datasource.max-active=10
spring.datasource.initial-size=5
spring.datasource.max-idle=5 spring.datasource.min-idle=1設定最大和最小空閒連線:
spring.datasource.test-while-idle=true spring.datasource.test-on-borrow=true啟用連線驗證:
spring.datasource.validation-query=SELECT 1 spring.datasource.time-between-eviction-runs-millis=5000 spring.datasource.min-evictable-idle-time-millis=60000指定驗證
HikariCP 的注意事項中Spring Boot 2.x,連接池已切換為HikariCP。如需更多設定選項,請參閱 HikariCP 文件。
以上是以下是一些標題選項,重點關注問答格式並涵蓋文章的內容: **選項 1(關注錯誤):** * **Spring Boot JPA/Hibernate:如何修復“Co”的詳細內容。更多資訊請關注PHP中文網其他相關文章!