Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjusting cancel and ensure close #2046

Merged
merged 7 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.Reader;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.net.SocketException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.sql.*;
Expand Down Expand Up @@ -68,23 +69,34 @@ public boolean next() throws SQLException {
@Override
public void close() throws SQLException {
closed = true;
if (reader != null) {
try {
reader.close();
} catch (Exception e) {
throw ExceptionUtils.toSqlState(e);
}

reader = null;
Exception e = null;
try {
if (reader != null) {
try {
reader.close();
} catch (Exception re) {
log.debug("Error closing reader", re);
e = re;
} finally {
reader = null;
}
}
} finally {
if (response != null) {
try {
response.close();
} catch (Exception re) {
log.debug("Error closing response", re);
e = re;
} finally {
response = null;
}
}
}

if (response != null) {
try {
response.close();
} catch (Exception e) {
throw ExceptionUtils.toSqlState(e);
}
response = null;
if (e != null) {
throw ExceptionUtils.toSqlState(e);
}
}

Expand Down
19 changes: 12 additions & 7 deletions jdbc-v2/src/main/java/com/clickhouse/jdbc/StatementImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class StatementImpl implements Statement, JdbcV2Wrapper {
private OperationMetrics metrics;
private List<String> batch;
private String lastSql;
private String lastQueryId;
private volatile String lastQueryId;
private String schema;
private int maxRows;

Expand Down Expand Up @@ -207,8 +207,13 @@ public int executeUpdate(String sql, QuerySettings settings) throws SQLException
public void close() throws SQLException {
closed = true;
if (currentResultSet != null) {
currentResultSet.close();
currentResultSet = null;
try {
currentResultSet.close();
} catch (Exception e) {
LOG.debug("Failed to close current result set", e);
} finally {
currentResultSet = null;
}
}
}

Expand Down Expand Up @@ -262,10 +267,10 @@ public void cancel() throws SQLException {
return;
}

try {
connection.client.query(String.format("KILL QUERY%sWHERE query_id = '%s'",
connection.onCluster ? " ON CLUSTER " + connection.cluster + " " : " ",
lastQueryId), connection.getDefaultQuerySettings()).get();
try (QueryResponse response = connection.client.query(String.format("KILL QUERY%sWHERE query_id = '%s'",
connection.onCluster ? " ON CLUSTER " + connection.cluster + " " : " ",
lastQueryId), connection.getDefaultQuerySettings()).get()){
Paultagoras marked this conversation as resolved.
Show resolved Hide resolved
LOG.debug("Query {} was killed by {}", lastQueryId, response.getQueryId());
} catch (Exception e) {
throw new SQLException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,12 @@ public static SQLException toSqlState(String message, Exception cause) {

return new SQLException(exceptionMessage, SQL_STATE_CLIENT_ERROR, cause);//Default
}

public static Throwable getRootCause(Throwable throwable) {
Throwable cause = throwable;
while (cause.getCause() != null) {
cause = cause.getCause();
}
return cause;
}
}
Loading