apache/dolphinscheduler · error · TaskException

Execute sql task failed

Error message

Execute sql task failed

What it means

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.

Source

Thrown at dolphinscheduler-task-plugin/dolphinscheduler-task-sql/src/main/java/org/apache/dolphinscheduler/plugin/task/sql/SqlTask.java:174

            List<SqlBinds> postStatementSqlBinds = Optional.ofNullable(sqlParameters.getPostStatements())
                    .orElse(new ArrayList<>())
                    .stream()
                    .map(this::getSqlAndSqlParamsMap)
                    .collect(Collectors.toList());

            // execute sql task
            executeFuncAndSql(mainStatementSqlBinds, preStatementSqlBinds, postStatementSqlBinds);

            setExitStatusCode(TaskConstants.EXIT_CODE_SUCCESS);

        } catch (Exception e) {
            if (exitStatusCode == TaskConstants.EXIT_CODE_KILL) {
                log.info("sql task has been killed");
                return;
            }
            setExitStatusCode(TaskConstants.EXIT_CODE_FAILURE);
            log.error("sql task error", e);
            throw new TaskException("Execute sql task failed", e);
        }
    }

    @Override
    public void cancel() throws TaskException {
        try {
            if (sessionStatement != null) {
                sessionStatement.cancel();
            }
            exitStatusCode = TaskConstants.EXIT_CODE_KILL;
        } catch (Exception e) {
            throw new TaskException("Cancel sql task failed", e);
        }
    }

    /**
     * execute function and sql
     *

View on GitHub (pinned to 02eac45a1b)

Solutions

  1. Read the wrapped cause in the worker log (logged as 'sql task error') — it names the actual JDBC/SQL failure
  2. 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
  3. If the statement is intentionally non-query or slow, adjust the task config (e.g. sqlType, timeout) so the executor expectations match the statement

Example fix

// before: statement references missing table
select * from tmp_report_daily  -- table does not exist
// after
select * from dwd_report_daily  -- or create the table first
Defensive patterns

Strategy: try-catch

Validate before calling

// before scheduling: verify connectivity + statement
dsApi.testConnection(datasourceId);
sqlClient.executeReadOnly("explain " + sql); // cheap syntax/object check

Try / catch

try {
    sqlTask.handle(callBack);
} catch (TaskException e) {
    Throwable cause = e.getCause(); // SQLException names the real DB error
    if (cause instanceof SQLTransientException) { /* retry */ }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "query failed", "%w: SQL error" — wrapped database query errors in Go libraries explained — this error's family across 3 libraries.

Related errors


AI-assisted analysis of apache/dolphinscheduler@02eac45a1b (2026-09-06). Data as JSON: /api/errors/fe9894f9e76e9648. Report an issue: GitHub.