Skip to content

Commit e905118

Browse files
author
Paultagoras
committed
Adjusting cancel and ensure close
1 parent e489e7a commit e905118

File tree

3 files changed

+46
-21
lines changed

3 files changed

+46
-21
lines changed

jdbc-v2/src/main/java/com/clickhouse/jdbc/ResultSetImpl.java

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.io.Reader;
77
import java.math.BigDecimal;
88
import java.net.MalformedURLException;
9+
import java.net.SocketException;
910
import java.net.URL;
1011
import java.nio.charset.StandardCharsets;
1112
import java.sql.*;
@@ -68,23 +69,34 @@ public boolean next() throws SQLException {
6869
@Override
6970
public void close() throws SQLException {
7071
closed = true;
71-
if (reader != null) {
72-
try {
73-
reader.close();
74-
} catch (Exception e) {
75-
throw ExceptionUtils.toSqlState(e);
76-
}
7772

78-
reader = null;
73+
Exception e = null;
74+
try {
75+
if (reader != null) {
76+
try {
77+
reader.close();
78+
} catch (Exception re) {
79+
log.debug("Error closing reader", re);
80+
e = re;
81+
} finally {
82+
reader = null;
83+
}
84+
}
85+
} finally {
86+
if (response != null) {
87+
try {
88+
response.close();
89+
} catch (Exception re) {
90+
log.debug("Error closing response", re);
91+
e = re;
92+
} finally {
93+
response = null;
94+
}
95+
}
7996
}
8097

81-
if (response != null) {
82-
try {
83-
response.close();
84-
} catch (Exception e) {
85-
throw ExceptionUtils.toSqlState(e);
86-
}
87-
response = null;
98+
if (e != null) {
99+
throw ExceptionUtils.toSqlState(e);
88100
}
89101
}
90102

jdbc-v2/src/main/java/com/clickhouse/jdbc/StatementImpl.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class StatementImpl implements Statement, JdbcV2Wrapper {
3232
private OperationMetrics metrics;
3333
private List<String> batch;
3434
private String lastSql;
35-
private String lastQueryId;
35+
private volatile String lastQueryId;
3636
private String schema;
3737
private int maxRows;
3838

@@ -207,8 +207,13 @@ public int executeUpdate(String sql, QuerySettings settings) throws SQLException
207207
public void close() throws SQLException {
208208
closed = true;
209209
if (currentResultSet != null) {
210-
currentResultSet.close();
211-
currentResultSet = null;
210+
try {
211+
currentResultSet.close();
212+
} catch (Exception e) {
213+
LOG.debug("Failed to close current result set", e);
214+
} finally {
215+
currentResultSet = null;
216+
}
212217
}
213218
}
214219

@@ -262,10 +267,10 @@ public void cancel() throws SQLException {
262267
return;
263268
}
264269

265-
try {
266-
connection.client.query(String.format("KILL QUERY%sWHERE query_id = '%s'",
267-
connection.onCluster ? " ON CLUSTER " + connection.cluster + " " : " ",
268-
lastQueryId), connection.getDefaultQuerySettings()).get();
270+
try (QueryResponse response = connection.client.query(String.format("KILL QUERY%sWHERE query_id = '%s'",
271+
connection.onCluster ? " ON CLUSTER " + connection.cluster + " " : " ",
272+
lastQueryId), connection.getDefaultQuerySettings()).get()){
273+
LOG.debug("Query {} was killed by {}", lastQueryId, response.getQueryId());
269274
} catch (Exception e) {
270275
throw new SQLException(e);
271276
}

jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/ExceptionUtils.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,12 @@ public static SQLException toSqlState(String message, Exception cause) {
6161

6262
return new SQLException(exceptionMessage, SQL_STATE_CLIENT_ERROR, cause);//Default
6363
}
64+
65+
public static Throwable getRootCause(Throwable throwable) {
66+
Throwable cause = throwable;
67+
while (cause.getCause() != null) {
68+
cause = cause.getCause();
69+
}
70+
return cause;
71+
}
6472
}

0 commit comments

Comments
 (0)