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

[jdbc-v2] Fixed closing prev result set #2041

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 13 additions & 0 deletions jdbc-v2/src/main/java/com/clickhouse/jdbc/StatementImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,18 @@ public ResultSetImpl executeQuery(String sql, QuerySettings settings) throws SQL
response = connection.client.query(lastSql, mergedSettings).get(queryTimeout, TimeUnit.SECONDS);
}
ClickHouseBinaryFormatReader reader = connection.client.newBinaryFormatReader(response);

if (currentResultSet != null) {
LOG.debug("Previous result set is open [resultSet = " + currentResultSet + "]");
// Closing request blindly assuming that user do not care about it anymore (DDL request for example)
try {
currentResultSet.close();
} catch (Exception e) {
LOG.error("Failed to close previous result set", e);
} finally {
currentResultSet = null;
}
}
currentResultSet = new ResultSetImpl(this, response, reader);
metrics = response.getMetrics();
lastQueryId = response.getQueryId();
Expand Down Expand Up @@ -289,6 +301,7 @@ private boolean execute(String sql, QuerySettings settings) throws SQLException
executeQuery(sql, settings); // keep open to allow getResultSet()
return true;
} else if(type == StatementType.SET) {
executeUpdate(sql, settings);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chernser
I don't think that we need to execute this update; the role URL setting is enough, as the HTTP interface is stateless.

//SET ROLE
List<String> tokens = JdbcUtils.tokenizeSQL(sql);
if (JdbcUtils.containsIgnoresCase(tokens, "ROLE")) {
Expand Down
26 changes: 24 additions & 2 deletions jdbc-v2/src/test/java/com/clickhouse/jdbc/StatementTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.clickhouse.jdbc;

import com.clickhouse.client.api.Client;
import com.clickhouse.client.api.ClientConfigProperties;
import com.clickhouse.client.api.query.GenericRecord;
import org.testng.Assert;
import org.testng.annotations.Test;
Expand All @@ -16,6 +18,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

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

try (ConnectionImpl conn = new ConnectionImpl(getEndpointString(), info)) {
GenericRecord record = conn.client.queryAll("SELECT currentRoles()").get(0);
assertEquals(record.getList(1).size(), 2);
assertEquals(record.getList(1).size(), 0);

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

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

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

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

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

record = conn.client.queryAll("SELECT currentRoles()").get(0);
assertEquals(record.getList(1).size(), 2);
assertEquals(record.getList(1).size(), 0);
}
}

Expand Down Expand Up @@ -449,4 +454,21 @@ public void testWithIPs() throws Exception {
}
}
}

@Test
public void testConnectionExhaustion() throws Exception {

int maxNumConnections = 3;
Properties properties = new Properties();
properties.put(ClientConfigProperties.HTTP_MAX_OPEN_CONNECTIONS.getKey(), "" + maxNumConnections);
properties.put(ClientConfigProperties.CONNECTION_REQUEST_TIMEOUT.getKey(), "" + 1000); // 1 sec connection req timeout

try (Connection conn = getJdbcConnection(properties)) {
try (Statement stmt = conn.createStatement()) {
for (int i = 0; i< maxNumConnections * 2; i++) {
stmt.executeQuery("SELECT number FROM system.numbers LIMIT 100");
}
}
}
}
}
Loading