


How to Prevent Database Connection Loss After 424 Hours in Spring Boot with Hibernate?
Oct 24, 2024 pm 06:38 PMResolving Database Connection Loss After 424 Hours in Spring Boot with Hibernate
An issue arises in a Spring Boot application using JPA-Hibernate with MySQL when the connection to the database is lost after a duration of 424 hours. The error log displays:
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.
To address this issue, it is recommended to configure appropriate connection properties in the application's configuration file (e.g., 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>
These settings enable the connection pool to:
- Establish a maximum number of active connections (spring.datasource.max-active).
- Validate connections periodically (spring.datasource.test-while-idle).
- Remove idle connections if they have not been used within a specified time (spring.datasource.min-evictable-idle-time-millis).
By implementing these configurations, the connection pool will regularly test the validity of connections and replace those that have become stale, ensuring stable database connectivity even after extended periods of inactivity.
The above is the detailed content of How to Prevent Database Connection Loss After 424 Hours in Spring Boot with Hibernate?. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte

How does Java's classloading mechanism work, including different classloaders and their delegation models?

Spring Boot SnakeYAML 2.0 CVE-2022-1471 Issue Fixed

Iceberg: The Future of Data Lake Tables

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?

Node.js 20: Key Performance Boosts and New Features

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?

How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?
