{"record":{"id":"d559d9b39f742c0f","repo":"apache/dolphinscheduler","slug":"get-current-version-from-database-error-sql","errorCode":null,"errorMessage":"Get current version from database error, sql: \" + sql","messagePattern":"Get current version from database error, sql: \" \\+ sql","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"dolphinscheduler-tools/src/main/java/org/apache/dolphinscheduler/tools/datasource/upgrader/UpgradeDao.java","lineNumber":83,"sourceCode":"        } catch (Exception ex) {\n            throw new RuntimeException(\"Execute initialize sql file: \" + sqlFilePath + \" error\", ex);\n        }\n    }\n\n    public String getCurrentVersion(String versionName) {\n        String sql = String.format(\"select version from %s\", versionName);\n        String version = null;\n        try (\n                Connection conn = dataSource.getConnection();\n                PreparedStatement pstmt = conn.prepareStatement(sql);\n                ResultSet rs = pstmt.executeQuery()) {\n            if (rs.next()) {\n                version = rs.getString(1);\n            }\n            return version;\n        } catch (SQLException e) {\n            log.error(\"Get current version from database error, sql: {}\", sql, e);\n            throw new RuntimeException(\"Get current version from database error, sql: \" + sql, e);\n        }\n    }\n\n    public void upgradeDolphinScheduler(String schemaDir) {\n        upgradeDolphinSchedulerDDL(schemaDir, \"dolphinscheduler_ddl.sql\");\n        upgradeDolphinSchedulerDML(schemaDir, \"dolphinscheduler_dml.sql\");\n    }\n\n    private void upgradeDolphinSchedulerDML(String schemaDir, String scriptFile) {\n        String schemaVersion = schemaDir.split(\"_\")[0];\n        String sqlFilePath =\n                String.format(\"sql/upgrade/%s/%s/%s\", schemaDir, dbType.getDb(), scriptFile);\n        try {\n            // Execute the upgraded dolphinscheduler dml\n            SqlScriptRunner sqlScriptRunner = new SqlScriptRunner(dataSource, sqlFilePath);\n            sqlScriptRunner.execute();\n            try (Connection connection = dataSource.getConnection()) {\n                String upgradeSQL;","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/apache/dolphinscheduler/blob/02eac45a1b6676e639fcbfb4be2243de5771b05d/dolphinscheduler-tools/src/main/java/org/apache/dolphinscheduler/tools/datasource/upgrader/UpgradeDao.java#L65-L101","documentation":"UpgradeDao.getCurrentVersion runs 'select version from <versionName>' (t_ds_version) to read the installed schema version. A SQLException during that query is rethrown as RuntimeException 'Get current version from database error, sql: <sql>'. It means the version lookup query itself failed against the database.","triggerScenarios":"Calling getCurrentVersion when the query throws SQLException: the t_ds_version table exists check passed but the query still fails due to permissions, connection drop, wrong catalog/schema, or a driver issue.","commonSituations":"Insufficient SELECT privilege on the version table; stale/dropped connection to the DB during a long upgrade; connecting with a user whose default schema differs from the one holding t_ds_version.","solutions":["Grant SELECT on t_ds_version to the database user used by the upgrade tool.","Test the exact SQL manually with the same JDBC user: SELECT version FROM t_ds_version;","Check DB connectivity (connection pool/timeout settings) and re-run the upgrade.","Inspect the cause SQLException for the vendor error code to pinpoint permissions vs connection issues."],"exampleFix":null,"handlingStrategy":"retry","validationCode":"// pre-check with the same connection/user\ntry (PreparedStatement ps = conn.prepareStatement(\"select version from t_ds_version\");\n     ResultSet rs = ps.executeQuery()) {\n    if (!rs.next()) log.warn(\"t_ds_version has no rows\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    String v = upgradeDao.getCurrentVersion(\"t_ds_version\");\n} catch (RuntimeException e) {\n    if (e.getCause() instanceof SQLException) {\n        log.error(\"Version query failed (check privileges/connection): {}\", e.getCause().getMessage());\n        // retry once after reconnect\n    } else throw e;\n}","preventionTips":["Grant SELECT on t_ds_version to the tool's DB user.","Use a connection pool with keepalive/validity checks for long upgrades.","Run the tool on the same network/host as the DB to reduce dropouts.","Confirm default schema/search_path includes the DolphinScheduler tables."],"tags":["database","sql","version"],"backgroundTag":"database-query-failed","analyzedSha":"02eac45a1b6676e639fcbfb4be2243de5771b05d","analyzedAt":"2026-09-06T17:43:00.555Z","contentChangedAt":"2026-09-06T17:43:00.555Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}