{"record":{"id":"a4a4dc51baec8998","repo":"apache/dolphinscheduler","slug":"cancel-sql-task-failed","errorCode":null,"errorMessage":"Cancel sql task failed","messagePattern":"Cancel 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":186,"sourceCode":"            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     *\n     * @param mainStatementsBinds main statements binds\n     * @param preStatementsBinds  pre statements binds\n     * @param postStatementsBinds post statements binds\n     */\n    public void executeFuncAndSql(List<SqlBinds> mainStatementsBinds,\n                                  List<SqlBinds> preStatementsBinds,\n                                  List<SqlBinds> postStatementsBinds) throws Exception {\n        try (\n                Connection connection =\n                        DataSourceClientProvider.getAdHocConnection(DbType.valueOf(sqlParameters.getType()),\n                                baseConnectionParam)) {\n            sessionConnection = connection;","sourceCodeStart":168,"sourceCodeEnd":204,"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#L168-L204","documentation":"SqlTask.cancel() attempts to cancel the running JDBC statement and sets the exit code to KILL. If any exception occurs while cancelling the session statement (or in the cancel block), the exception is wrapped and rethrown as a TaskException with this message.","triggerScenarios":"Calling cancel on a SqlTask whose underlying Statement.cancel() throws (e.g. the connection is already closed, the driver does not support cancellation, or a network drop to the database occurred during the cancel request).","commonSituations":"Killing a long-running SQL task from the DS UI; worker shutdown/fault tolerance interrupting tasks; database failover mid-cancel; drivers (e.g. some Hive/JDBC versions) that throw on cancel of finished statements.","solutions":["Check database connectivity and driver support for Statement.cancel(); upgrade the JDBC driver if cancel is unsupported","Inspect the wrapped cause exception in the task log to identify the real failure (closed connection, timeout, etc.)","Retry the kill; if the statement already completed, the task result is usually still valid","Ensure the task's datasource connection pool is not forcibly closing connections before cancel is invoked"],"exampleFix":"// before\ntry {\n    sessionStatement.cancel();\n} catch (Exception e) {\n    throw new TaskException(\"Cancel sql task failed\", e);\n}\n// after\ntry {\n    if (sessionStatement != null && !sessionStatement.isClosed()) {\n        sessionStatement.cancel();\n    }\n} catch (Exception e) {\n    log.warn(\"Cancel sql task failed, statement may already be closed\", e);\n    exitStatusCode = TaskConstants.EXIT_CODE_KILL;\n}","handlingStrategy":"try-catch","validationCode":"if (sessionStatement != null) {\n    try {\n        sessionStatement.isClosed(); // probe connection health before cancel\n    } catch (Exception e) {\n        log.warn(\"Statement/connection already broken, skip cancel\");\n    }\n}","typeGuard":"boolean cancellable(java.sql.Statement st) {\n    try { return st != null && !st.isClosed(); } catch (Exception e) { return false; }\n}","tryCatchPattern":"try {\n    sqlTask.cancel();\n} catch (TaskException e) {\n    // inspect e.getCause(): SQLRecoverableException -> connection lost; SQLFeatureNotSupported -> driver can't cancel\n    log.error(\"Cancel failed, cause: {}\", e.getCause(), e);\n}","preventionTips":["Use JDBC drivers with reliable Statement.cancel() support","Avoid killing tasks during database failover windows","Monitor datasource connectivity so cancels don't hit closed connections"],"tags":["sql","jdbc","task-cancel"],"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"}