Skip to content

Commit e95686f

Browse files
committed
fix(console): improve cancellation
1 parent e7b82c2 commit e95686f

2 files changed

Lines changed: 16 additions & 7 deletions

File tree

backend/framework/src/main/java/org/jumpserver/chen/framework/console/QueryConsole.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ public void onCancel() {
617617
this.getState().setExecutionStatus(EXECUTION_STATUS_CANCELLED);
618618
try {
619619
var plan = this.currentPlan;
620-
if (plan != null && plan.getStatement() != null) {
620+
if (plan != null) {
621621
plan.cancel();
622622
this.getConsoleLogger().error("cancel query: %s", plan.getTargetSQL());
623623
}
@@ -893,15 +893,13 @@ private DataView runSingleSQL(String sql, ACLResult aclResult) throws SQLExcepti
893893
.createPlan(SQL.of(sourceSQL));
894894
plan.setAclResult(aclResult);
895895
plan.setSqlQueryParams(sqlQueryParams);
896-
plan.generateTargetSQL();
897-
this.getConsoleLogger().info("execute sql: %s", plan.getTargetSQL());
898-
899896
this.currentPlan = plan;
900-
901897
this.getState().setCanCancel(true);
902898
this.stateManager.commit();
903899

904900
try {
901+
plan.generateTargetSQL();
902+
this.getConsoleLogger().info("execute sql: %s", plan.getTargetSQL());
905903
var result = plan.executeWithAudit();
906904
this.getConsoleLogger().success(result);
907905
return result;

backend/framework/src/main/java/org/jumpserver/chen/framework/datasource/sql/SQLExecutePlan.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class SQLExecutePlan {
2727
private String targetSQL;
2828
private final DbType druidDbType;
2929
private volatile Statement statement;
30+
private volatile boolean cancelled;
3031
private Connection connection;
3132
private ACLResult aclResult;
3233

@@ -85,7 +86,10 @@ public SQLQueryResult executeWithAudit() throws SQLException {
8586
}
8687

8788

88-
public Statement createStatement() throws SQLException {
89+
public synchronized Statement createStatement() throws SQLException {
90+
if (this.cancelled) {
91+
throw new SQLException(MessageUtils.get("ExecutionCanceled"));
92+
}
8993
if (this.statement == null || this.statement.isClosed()) {
9094
this.statement = this.connection.createStatement();
9195
}
@@ -97,7 +101,14 @@ public SQLStatement getTargetSQLStatement() {
97101
}
98102

99103
public void cancel() throws SQLException {
100-
this.statement.cancel();
104+
Statement currentStatement;
105+
synchronized (this) {
106+
this.cancelled = true;
107+
currentStatement = this.statement;
108+
}
109+
if (currentStatement != null && !currentStatement.isClosed()) {
110+
currentStatement.cancel();
111+
}
101112
}
102113

103114
public void close() {

0 commit comments

Comments
 (0)