{"record":{"id":"a03b9ae41c7b5408","repo":"alibaba/druid","slug":"validationquery-failed","errorCode":null,"errorMessage":"validationQuery failed","messagePattern":"validationQuery failed","errorType":"exception","errorClass":"SQLException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/com/alibaba/druid/pool/DruidAbstractDataSource.java","lineNumber":1482,"sourceCode":"            }\n\n            if (!result) {\n                SQLException sqlError = error != null ? //\n                        new SQLException(\"validateConnection false\", error) //\n                        : new SQLException(\"validateConnection false\");\n                throw sqlError;\n            }\n            return;\n        }\n\n        if (null != query) {\n            boolean valid;\n            try {\n                valid = ValidConnectionCheckerAdapter.execValidQuery(conn, query, validationQueryTimeout);\n            } catch (SQLException ex) {\n                throw ex;\n            } catch (Exception ex) {\n                throw new SQLException(\"validationQuery failed\", ex);\n            } finally {\n                if (conn instanceof ConnectionProxyImpl) {\n                    ((ConnectionProxyImpl) conn).setLastValidateTimeMillis(System.currentTimeMillis());\n                }\n            }\n\n            if (!valid) {\n                throw new SQLException(\"validationQuery didn't return a row\");\n            }\n\n            if (onFatalError) {\n                lock.lock();\n                try {\n                    if (onFatalError) {\n                        onFatalError = false;\n                    }\n                } finally {\n                    lock.unlock();","sourceCodeStart":1464,"sourceCodeEnd":1500,"githubUrl":"https://github.com/alibaba/druid/blob/fa8dc9912637a2f729eef9f55356621fec18d40e/core/src/main/java/com/alibaba/druid/pool/DruidAbstractDataSource.java#L1464-L1500","documentation":"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.).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the correct JDBC driver is on the classpath so Druid auto-registers the dialect-specific validConnectionChecker (avoiding the generic adapter path).","Use a supported, simple validationQuery for your DB (e.g. 'SELECT 1' for MySQL/PG).","Read SQLException.getCause() to find the underlying non-SQL exception and fix the root cause.","Confirm validationQueryTimeout is positive and reasonable."],"exampleFix":"// before\n// ds.setValidationQuery(\"SELECT 1 FROM dual\"); // wrong for non-Oracle, driver throws\n\n// after\n// ds.setValidationQuery(\"SELECT 1\"); // MySQL/PostgreSQL\n// (and ensure the matching JDBC driver JAR is present so a checker is auto-registered)","handlingStrategy":"validation","validationCode":"// Ensure a dialect-specific validConnectionChecker is registered so the generic adapter path is avoided:\n// ds.setValidationQuery(\"SELECT 1\"); // match your DB\n// ds.setValidConnectionChecker(new com.alibaba.druid.pool.vendor.MySqlValidConnectionChecker()); // example\n// Read SQLException.getCause() when it occurs to find the non-SQL exception.","typeGuard":null,"tryCatchPattern":"try {\n    ds.validateConnection(conn);\n} catch (SQLException e) {\n    if (\"validationQuery failed\".equals(e.getMessage())) {\n        Throwable cause = e.getCause(); // non-SQL exception from the adapter/driver\n        // ensure correct JDBC driver JAR + matching checker; simplify validationQuery\n    }\n    throw e;\n}","preventionTips":["Keep the correct JDBC driver JAR so Druid auto-registers a dialect-specific validConnectionChecker.","Use a simple, DB-appropriate validationQuery ('SELECT 1' for MySQL/PG).","Inspect getCause() to find the underlying non-SQL exception and fix its root cause."],"tags":["datasource","connection-validation","validation-query","configuration"],"backgroundTag":null,"analyzedSha":"fa8dc9912637a2f729eef9f55356621fec18d40e","analyzedAt":"2026-08-14T04:55:06.789Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}