{"record":{"id":"318d00d425d03694","repo":"alibaba/druid","slug":"validationquery-didn-t-return-a-row","errorCode":null,"errorMessage":"validationQuery didn't return a row","messagePattern":"validationQuery didn't return a row","errorType":"exception","errorClass":"SQLException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/com/alibaba/druid/pool/DruidAbstractDataSource.java","lineNumber":1490,"sourceCode":"            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();\n                }\n            }\n        }\n    }\n\n    /**\n     * @deprecated\n     */","sourceCodeStart":1472,"sourceCodeEnd":1508,"githubUrl":"https://github.com/alibaba/druid/blob/fa8dc9912637a2f729eef9f55356621fec18d40e/core/src/main/java/com/alibaba/druid/pool/DruidAbstractDataSource.java#L1472-L1508","documentation":"Thrown during connection validation when a configured validationQuery is executed via ValidConnectionCheckerAdapter.execValidQuery but the query returns no rows. Druid uses the validation query (e.g. SELECT 1) to confirm a connection is alive; a query that returns zero rows is treated as proof the connection is unusable. This guard runs on testWhileIdle, testOnBorrow, testOnReturn, and keepAlive paths.","triggerScenarios":"validateConnection(conn) is called with a non-null query, execValidQuery returns false (empty ResultSet), and the !valid branch at line 1489 fires. Happens when validationQuery is set to something that conditionally returns no rows, or when the DB session is in a state where the query yields nothing.","commonSituations":"A validationQuery like 'SELECT 1 FROM dual WHERE 1=0' that deliberately returns no rows; a query referencing a table/object the connecting user lacks privileges on; a stale connection whose session state makes the validation query return empty; custom validation queries ported from another DB dialect.","solutions":["Set validationQuery to a statement guaranteed to return exactly one row for your database (MySQL: 'SELECT 1', Oracle: 'SELECT 1 FROM DUAL', PostgreSQL: 'SELECT 1').","Verify the DB user has SELECT privileges on whatever the validationQuery references.","Prefer using a ValidConnectionChecker (Druid auto-selects one per dbType) by leaving validationQuery default rather than overriding it with a fragile custom query.","If using testWhileIdle/keepAlive, confirm the validation query works by running it manually in the same session context."],"exampleFix":"// before\ndataSource.setValidationQuery(\"SELECT 1 FROM app_config WHERE flag='on'\");\n// after\ndataSource.setValidationQuery(\"SELECT 1\");","handlingStrategy":"try-catch","validationCode":"// Confirm the validation query returns a row in your DB before assigning it\nString vq = dataSource.getValidationQuery();\ntry (Connection c = dataSource.getDriver().connect(dataSource.getUrl(), dataSource.getConnectProperties());\n     Statement s = c.createStatement();\n     ResultSet rs = s.executeQuery(vq)) {\n    if (!rs.next()) {\n        throw new IllegalStateException(\"validationQuery returns no rows: \" + vq);\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    conn = dataSource.getConnection();\n} catch (SQLException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"validationQuery didn't return a row\")) {\n        LOG.error(\"validationQuery misconfigured; check \" + dataSource.getValidationQuery(), e);\n    }\n    throw e;\n}","preventionTips":["Use the canonical single-row validation query for your DB dialect.","Leave validationQuery at default so Druid's built-in ValidConnectionChecker is used.","Never reference application tables in the validation query."],"tags":["validation","connection-pool","config","keepalive"],"backgroundTag":null,"analyzedSha":"fa8dc9912637a2f729eef9f55356621fec18d40e","analyzedAt":"2026-08-14T04:55:06.789Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}