{"record":{"id":"fe9894f9e76e9648","repo":"apache/dolphinscheduler","slug":"execute-sql-task-failed","errorCode":null,"errorMessage":"Execute sql task failed","messagePattern":"Execute sql task failed","errorType":"exception","errorClass":"TaskException","httpStatus":null,"severity":"error","filePath":"dolphinscheduler-task-plugin/dolphinscheduler-task-sql/src/main/java/org/apache/dolphinscheduler/plugin/task/sql/SqlTask.java","lineNumber":174,"sourceCode":"            List<SqlBinds> postStatementSqlBinds = Optional.ofNullable(sqlParameters.getPostStatements())\n                    .orElse(new ArrayList<>())\n                    .stream()\n                    .map(this::getSqlAndSqlParamsMap)\n                    .collect(Collectors.toList());\n\n            // execute sql task\n            executeFuncAndSql(mainStatementSqlBinds, preStatementSqlBinds, postStatementSqlBinds);\n\n            setExitStatusCode(TaskConstants.EXIT_CODE_SUCCESS);\n\n        } catch (Exception e) {\n            if (exitStatusCode == TaskConstants.EXIT_CODE_KILL) {\n                log.info(\"sql task has been killed\");\n                return;\n            }\n            setExitStatusCode(TaskConstants.EXIT_CODE_FAILURE);\n            log.error(\"sql task error\", e);\n            throw new TaskException(\"Execute sql task failed\", e);\n        }\n    }\n\n    @Override\n    public void cancel() throws TaskException {\n        try {\n            if (sessionStatement != null) {\n                sessionStatement.cancel();\n            }\n            exitStatusCode = TaskConstants.EXIT_CODE_KILL;\n        } catch (Exception e) {\n            throw new TaskException(\"Cancel sql task failed\", e);\n        }\n    }\n\n    /**\n     * execute function and sql\n     *","sourceCodeStart":156,"sourceCodeEnd":192,"githubUrl":"https://github.com/apache/dolphinscheduler/blob/02eac45a1b6676e639fcbfb4be2243de5771b05d/dolphinscheduler-task-plugin/dolphinscheduler-task-sql/src/main/java/org/apache/dolphinscheduler/plugin/task/sql/SqlTask.java#L156-L192","documentation":"The catch-all in SqlTask.handle(): any exception executing the statement (JDBC connection failure, SQL syntax error, result-fetch failure) other than an explicit kill is caught, the exit status is set to EXIT_CODE_FAILURE, and a TaskException(\"Execute sql task failed\", e) is thrown. The root cause (e.g. SQLException) is in the wrapped cause; if the task was killed (EXIT_CODE_KILL) it returns quietly instead.","triggerScenarios":"JDBC executeQuery/execute fails: unreachable datasource host/port, wrong credentials, malformed SQL, syntax not supported by the target DB, query timeout, or errors thrown while collecting results into the output parameter.","commonSituations":"Datasource host unreachable from the worker network or firewall-blocked; SQL contains a typo or references a missing table/column; datasource password rotated after the task was saved; non-SELECT statement executed where a query result is expected.","solutions":["Read the wrapped cause in the worker log (logged as 'sql task error') — it names the actual JDBC/SQL failure","Test the SQL directly against the datasource (Datasource Center 'Test Connection' + run the statement in a SQL client) to separate SQL bugs from connectivity issues","If the statement is intentionally non-query or slow, adjust the task config (e.g. sqlType, timeout) so the executor expectations match the statement"],"exampleFix":"// before: statement references missing table\nselect * from tmp_report_daily  -- table does not exist\n// after\nselect * from dwd_report_daily  -- or create the table first","handlingStrategy":"try-catch","validationCode":"// before scheduling: verify connectivity + statement\ndsApi.testConnection(datasourceId);\nsqlClient.executeReadOnly(\"explain \" + sql); // cheap syntax/object check","typeGuard":null,"tryCatchPattern":"try {\n    sqlTask.handle(callBack);\n} catch (TaskException e) {\n    Throwable cause = e.getCause(); // SQLException names the real DB error\n    if (cause instanceof SQLTransientException) { /* retry */ }\n}","preventionTips":["Use Datasource Center 'Test Connection' before scheduling tasks against a datasource","Lint/validate SQL in CI and verify referenced tables exist","Set realistic query timeouts and prefer retry only for transient (connection/timeout) causes, never for syntax errors"],"tags":["sql","jdbc","task-execution"],"backgroundTag":"sql-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"}