Skip to content

Commit 0760386

Browse files
authored
Merge pull request #2041 from ClickHouse/jdbc2_fix_connections
[jdbc-v2] Fixed closing prev result set
2 parents 181f489 + e0840c1 commit 0760386

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,18 @@ public ResultSetImpl executeQuery(String sql, QuerySettings settings) throws SQL
152152
response = connection.client.query(lastSql, mergedSettings).get(queryTimeout, TimeUnit.SECONDS);
153153
}
154154
ClickHouseBinaryFormatReader reader = connection.client.newBinaryFormatReader(response);
155+
156+
if (currentResultSet != null) {
157+
LOG.debug("Previous result set is open [resultSet = " + currentResultSet + "]");
158+
// Closing request blindly assuming that user do not care about it anymore (DDL request for example)
159+
try {
160+
currentResultSet.close();
161+
} catch (Exception e) {
162+
LOG.error("Failed to close previous result set", e);
163+
} finally {
164+
currentResultSet = null;
165+
}
166+
}
155167
currentResultSet = new ResultSetImpl(this, response, reader);
156168
metrics = response.getMetrics();
157169
lastQueryId = response.getQueryId();
@@ -289,6 +301,7 @@ private boolean execute(String sql, QuerySettings settings) throws SQLException
289301
executeQuery(sql, settings); // keep open to allow getResultSet()
290302
return true;
291303
} else if(type == StatementType.SET) {
304+
executeUpdate(sql, settings);
292305
//SET ROLE
293306
List<String> tokens = JdbcUtils.tokenizeSQL(sql);
294307
if (JdbcUtils.containsIgnoresCase(tokens, "ROLE")) {

jdbc-v2/src/test/java/com/clickhouse/jdbc/StatementTest.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.clickhouse.jdbc;
22

3+
import com.clickhouse.client.api.Client;
4+
import com.clickhouse.client.api.ClientConfigProperties;
35
import com.clickhouse.client.api.query.GenericRecord;
46
import org.testng.Assert;
57
import org.testng.annotations.Test;
@@ -16,6 +18,7 @@
1618
import java.util.Arrays;
1719
import java.util.List;
1820
import java.util.Properties;
21+
import java.util.concurrent.TimeUnit;
1922

2023
import static org.testng.Assert.*;
2124
import static org.testng.Assert.assertEquals;
@@ -361,28 +364,30 @@ private void testSettingRole() throws SQLException {
361364

362365
try (ConnectionImpl conn = new ConnectionImpl(getEndpointString(), info)) {
363366
GenericRecord record = conn.client.queryAll("SELECT currentRoles()").get(0);
364-
assertEquals(record.getList(1).size(), 2);
367+
assertEquals(record.getList(1).size(), 0);
365368

366369
try (Statement stmt = conn.createStatement()) {
367370
stmt.execute("SET ROLE role1");
368371
}
369372

370373
record = conn.client.queryAll("SELECT currentRoles()").get(0);
371374
assertEquals(record.getList(1).size(), 1);
375+
assertEquals(record.getList(1).get(0), "role1");
372376

373377
try (Statement stmt = conn.createStatement()) {
374378
stmt.execute("SET ROLE role2");
375379
}
376380

377381
record = conn.client.queryAll("SELECT currentRoles()").get(0);
378382
assertEquals(record.getList(1).size(), 1);
383+
assertEquals(record.getList(1).get(0), "role2");
379384

380385
try (Statement stmt = conn.createStatement()) {
381386
stmt.execute("SET ROLE NONE");
382387
}
383388

384389
record = conn.client.queryAll("SELECT currentRoles()").get(0);
385-
assertEquals(record.getList(1).size(), 2);
390+
assertEquals(record.getList(1).size(), 0);
386391
}
387392
}
388393

@@ -449,4 +454,21 @@ public void testWithIPs() throws Exception {
449454
}
450455
}
451456
}
457+
458+
@Test
459+
public void testConnectionExhaustion() throws Exception {
460+
461+
int maxNumConnections = 3;
462+
Properties properties = new Properties();
463+
properties.put(ClientConfigProperties.HTTP_MAX_OPEN_CONNECTIONS.getKey(), "" + maxNumConnections);
464+
properties.put(ClientConfigProperties.CONNECTION_REQUEST_TIMEOUT.getKey(), "" + 1000); // 1 sec connection req timeout
465+
466+
try (Connection conn = getJdbcConnection(properties)) {
467+
try (Statement stmt = conn.createStatement()) {
468+
for (int i = 0; i< maxNumConnections * 2; i++) {
469+
stmt.executeQuery("SELECT number FROM system.numbers LIMIT 100");
470+
}
471+
}
472+
}
473+
}
452474
}

0 commit comments

Comments
 (0)