首頁 > Java > java教程 > 主體

如何在使用 Hibernate 的 Spring Boot 中防止 424 小時後資料庫連線遺失?

Patricia Arquette
發布: 2024-10-24 18:38:06
原創
221 人瀏覽過

How to Prevent Database Connection Loss After 424 Hours in Spring Boot with Hibernate?

使用Hibernate 解決Spring Boot 424 小時後資料庫連線遺失的問題

使用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>
登入後複製

這些設定使連結池能夠:

  • 建立最大數量的活動連結(spring.datasource.max-active)。
  • 定期驗證連線 (spring.datasource.test-while) -idle)。
  • 如果在指定時間內未使用空閒連接,則將其刪除 (spring.datasource.min-evictable-idle-time-millis)。

透過實現透過這些配置,連接池將定期測試連接的有效性並替換已過時的連接,確保即使在長時間不活動後也能保持穩定的資料庫連接。

以上是如何在使用 Hibernate 的 Spring Boot 中防止 424 小時後資料庫連線遺失?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!