alibaba/druid · info · SQLException

validateConnection: connection closed

Error message

validateConnection: connection closed

What it means

validateConnection(Connection) probes conn.isClosed() before running the validation query/validConnectionChecker and throws SQLException("validateConnection: connection closed") when the driver reports the connection as already closed. This is an operational signal, not a configuration defect: the physical connection died (server-side timeout, network drop, DB restart, manual close) and must be discarded by the pool.

Source

Thrown at core/src/main/java/com/alibaba/druid/pool/DruidAbstractDataSource.java:1437

        String[] filterArray = filters.split("\\,");

        for (String item : filterArray) {
            FilterManager.loadFilter(this.filters, item.trim());
        }
    }

    public void clearFilters() {
        if (!isClearFiltersEnable()) {
            return;
        }
        this.filters.clear();
    }

    public void validateConnection(Connection conn) throws SQLException {
        String query = getValidationQuery();
        if (conn.isClosed()) {
            throw new SQLException("validateConnection: connection closed");
        }

        if (validConnectionChecker != null) {
            boolean result;
            Exception error = null;
            try {
                result = validConnectionChecker.isValidConnection(conn, validationQuery, validationQueryTimeout);

                if (conn instanceof ConnectionProxyImpl) {
                    ((ConnectionProxyImpl) conn).setLastValidateTimeMillis(System.currentTimeMillis());
                }
                if (result && onFatalError) {
                    lock.lock();
                    try {
                        if (onFatalError) {
                            onFatalError = false;
                        }
                    } finally {

View on GitHub (pinned to fa8dc99126)

Solutions

  1. Tune maxEvictableIdleTimeMillis / keepAlive to be shorter than the DB/server idle timeout so connections are evicted/kept-alive before the server closes them.
  2. Ensure keepAliveBetweenTimeMillis and validationQuery are configured so dead connections are detected and replaced.
  3. If seen repeatedly in logs, investigate network/firewall idle reaping or DB wait_timeout.
  4. This exception is normally handled internally by the pool; no app code change is needed unless it surfaces to callers.
Defensive patterns

Strategy: retry

Validate before calling

// Pre-flight check before relying on a connection from the pool:
// if (conn == null || conn.isClosed()) { request a fresh connection / let the pool replace it }
// Configure the pool so this is rare:
// ds.setMaxEvictableIdleTimeMillis(< db wait_timeout); ds.setKeepAlive(true);

Try / catch

// This is normally handled inside the pool. If you call validateConnection directly:
try {
    ds.validateConnection(conn);
} catch (SQLException e) {
    if ("validateConnection: connection closed".equals(e.getMessage())) {
        // discard conn, obtain a new one from the pool
    }
}

Prevention

When it happens

Trigger: Druid's keep-alive/validate task calls validateConnection on a pooled connection whose driver reports isClosed()==true; or a user calls validateConnection directly on a closed connection.

Common situations: Database or network restarted idle connections; an external firewall/LB reaped idle TCP connections; the server's wait_timeout is shorter than Druid's eviction interval; a connection was explicitly closed elsewhere.

Understand the failure class

Related errors


AI-assisted analysis of alibaba/druid@fa8dc99126 (2026-08-14). Data as JSON: /api/errors/0d9d26adb27c00d8. Report an issue: GitHub.