ホームページ  >  記事  >  Java  >  Hibernate を使用した Spring Boot で 424 時間後のデータベース接続の損失を防ぐ方法は?

Hibernate を使用した Spring Boot で 424 時間後のデータベース接続の損失を防ぐ方法は?

Patricia Arquette
Patricia Arquetteオリジナル
2024-10-24 18:38:06203ブラウズ

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

Hibernate を使用した Spring Boot で 424 時間後のデータベース接続損失の解決

MySQL で JPA-Hibernate を使用する 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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。