使用JPA-Hibernate 和MySQL 的Spring Boot 應用程式在連接到資料庫時出現問題424小時後丟失。錯誤日誌顯示:
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'. 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.
要解決此問題,建議在應用程式的設定檔中設定適當的連線屬性(例如application.properties):
<code class="properties"># Connection validation and pool configuration spring.datasource.max-active=10 spring.datasource.initial-size=5 spring.datasource.max-idle=5 spring.datasource.min-idle=1 # Periodic connection validation spring.datasource.test-while-idle=true spring.datasource.validation-query=SELECT 1 # Idle connection management spring.datasource.time-between-eviction-runs-millis=5000 spring.datasource.min-evictable-idle-time-millis=60000</code>
這些設定使連結池能夠:
透過實現透過這些配置,連接池將定期測試連接的有效性並替換已過時的連接,確保即使在長時間不活動後也能保持穩定的資料庫連接。
以上是如何在使用 Hibernate 的 Spring Boot 中防止 424 小時後資料庫連線遺失?的詳細內容。更多資訊請關注PHP中文網其他相關文章!