>  기사  >  Java  >  Hibernate를 사용하여 Spring 부팅에서 424시간 후 데이터베이스 연결 손실을 방지하는 방법은 무엇입니까?

Hibernate를 사용하여 Spring 부팅에서 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 부팅에서 424시간 후 데이터베이스 연결 손실을 방지하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.