SoFunction
Updated on 2025-04-13

Solve the warning of "discard long time none received connection" in Spring Boot

When using Spring Boot combined with Druid connection pooling, developers may encounter a warning message "discard long time none received connection". While this does not usually affect the normal operation of the application, these warning messages can be troublesome. This article will explore the reasons for this problem and provide several solutions.

Problem phenomenon

When using new versions of Druid connection pools (such as 1.2.5), the console may print the following warning from time to time:

WARN   - discard long time none received connection. , jdbcUrl : [your-jdbc-url]

This indicates that the Druid connection pool detects that some database connections have been active for a certain period of time and are therefore marked as idle for a long time and discarded.

Cause

After checking Druid's source code, it was found that the root of this problem lies in the processing logic of Druid connection pool for MySQL connections. existDruidAbstractDataSourceClassictestConnectionInternalIn the method, if the connection is idle for more than 60 seconds, Druid will think that the connection is no longer valid and discard it.

if (valid && isMySql) {
    long lastPacketReceivedTimeMs = (conn);
    if (lastPacketReceivedTimeMs > 0 && 
        mysqlIdleMillis >= timeBetweenEvictionRunsMillis) {
        discardConnection(holder);
        ("discard long time none received connection. " +
                 ", jdbcUrl : " + jdbcUrl + ", version : " + () +
                 ", lastPacketReceivedIdleMillis : " + mysqlIdleMillis);
        return false;
    }
}

Solution

1. Modify Druid configuration

One solution is to modify the configuration of Druid by setting=falseTo avoid using MySQL's Ping method to check connection validity. This can be achieved in two ways:

  • Runtime configuration: Added to the running parameters-=false
  • Spring configuration: Added in Spring's configuration file:
spring:
  datasource:
    druid:
      mysql:
        usePingMethod: false

2. Adjust database configuration

Another way is to adjust the configuration of the database, such as MySQLwait_timeoutParameters to increase the idle timeout time on the database side, thereby reducing the situation where Druid errors are reported due to the database side closing the connection.

3. Code-level optimization

Finally, make sure that the application can release resources in a timely manner when using database connections to avoid unnecessary connection leakage.

Conclusion

While the warning "discard long time none received connection" may not directly affect the operation of the application, solving this problem can improve application stability and reduce unnecessary waste of resources. Hope the methods provided in this article can help you solve this problem.

This is the article about solving the "discard long time none received connection" warning in Spring Boot. This is all about this article. For more related Spring Boot Druid connection pool warning content, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!