alibaba/druid · error · SQLException

validationQuery failed

Error message

validationQuery failed

What it means

In validateConnection, when there is no validConnectionChecker but a validationQuery is set, Druid runs ValidConnectionCheckerAdapter.execValidQuery(conn, query, timeout). A SQLException from that call is rethrown unchanged, but any other Exception is wrapped as SQLException("validationQuery failed", ex). This indicates the validation query itself errored for a non-SQL reason (driver bug, NPE in the adapter, timeout as a runtime exception, etc.).

Source

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

            }

            if (!result) {
                SQLException sqlError = error != null ? //
                        new SQLException("validateConnection false", error) //
                        : new SQLException("validateConnection false");
                throw sqlError;
            }
            return;
        }

        if (null != query) {
            boolean valid;
            try {
                valid = ValidConnectionCheckerAdapter.execValidQuery(conn, query, validationQueryTimeout);
            } catch (SQLException ex) {
                throw ex;
            } catch (Exception ex) {
                throw new SQLException("validationQuery failed", ex);
            } finally {
                if (conn instanceof ConnectionProxyImpl) {
                    ((ConnectionProxyImpl) conn).setLastValidateTimeMillis(System.currentTimeMillis());
                }
            }

            if (!valid) {
                throw new SQLException("validationQuery didn't return a row");
            }

            if (onFatalError) {
                lock.lock();
                try {
                    if (onFatalError) {
                        onFatalError = false;
                    }
                } finally {
                    lock.unlock();

View on GitHub (pinned to fa8dc99126)

Solutions

  1. Ensure the correct JDBC driver is on the classpath so Druid auto-registers the dialect-specific validConnectionChecker (avoiding the generic adapter path).
  2. Use a supported, simple validationQuery for your DB (e.g. 'SELECT 1' for MySQL/PG).
  3. Read SQLException.getCause() to find the underlying non-SQL exception and fix the root cause.
  4. Confirm validationQueryTimeout is positive and reasonable.

Example fix

// before
// ds.setValidationQuery("SELECT 1 FROM dual"); // wrong for non-Oracle, driver throws

// after
// ds.setValidationQuery("SELECT 1"); // MySQL/PostgreSQL
// (and ensure the matching JDBC driver JAR is present so a checker is auto-registered)
Defensive patterns

Strategy: validation

Validate before calling

// Ensure a dialect-specific validConnectionChecker is registered so the generic adapter path is avoided:
// ds.setValidationQuery("SELECT 1"); // match your DB
// ds.setValidConnectionChecker(new com.alibaba.druid.pool.vendor.MySqlValidConnectionChecker()); // example
// Read SQLException.getCause() when it occurs to find the non-SQL exception.

Try / catch

try {
    ds.validateConnection(conn);
} catch (SQLException e) {
    if ("validationQuery failed".equals(e.getMessage())) {
        Throwable cause = e.getCause(); // non-SQL exception from the adapter/driver
        // ensure correct JDBC driver JAR + matching checker; simplify validationQuery
    }
    throw e;
}

Prevention

When it happens

Trigger: validationQuery is set, validConnectionChecker is null, and executing the query throws something that is not a SQLException - e.g. a NullPointerException inside the driver/adapter, an IllegalMonitorStateException, or a wrapped timeout.

Common situations: A dialect-specific validConnectionChecker is not registered (so the generic adapter is used) and the driver throws a non-SQL exception on the validation statement; a malformed validationQuery that the driver rejects with a non-SQL exception; JDBC driver bug.

Related errors


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