diff --git a/jdbc-v2/src/main/java/com/clickhouse/jdbc/StatementImpl.java b/jdbc-v2/src/main/java/com/clickhouse/jdbc/StatementImpl.java index f50546393..2e41cc14a 100644 --- a/jdbc-v2/src/main/java/com/clickhouse/jdbc/StatementImpl.java +++ b/jdbc-v2/src/main/java/com/clickhouse/jdbc/StatementImpl.java @@ -174,7 +174,10 @@ protected ResultSetImpl executeQueryImpl(String sql, QuerySettings settings) thr if (queryTimeout == 0) { response = connection.getClient().query(lastStatementSql, mergedSettings).get(); } else { - response = connection.getClient().query(lastStatementSql, mergedSettings).get(queryTimeout, TimeUnit.SECONDS); + // we need to perform async operation to support timeout. + response = connection.getClient().query(lastStatementSql, mergedSettings + .setOption(ClientConfigProperties.ASYNC_OPERATIONS.getKey(), true)) + .get(queryTimeout, TimeUnit.SECONDS); } if (response.getFormat().isText()) { @@ -246,7 +249,9 @@ protected long executeUpdateImpl(String sql, QuerySettings settings) throws SQLE LOG.trace("SQL Query: {}", lastStatementSql); int updateCount = 0; try (QueryResponse response = queryTimeout == 0 ? connection.getClient().query(lastStatementSql, mergedSettings).get() - : connection.getClient().query(lastStatementSql, mergedSettings).get(queryTimeout, TimeUnit.SECONDS)) { + : connection.getClient().query(lastStatementSql, mergedSettings + .setOption(ClientConfigProperties.ASYNC_OPERATIONS.getKey(), true)) + .get(queryTimeout, TimeUnit.SECONDS)) { updateCount = Math.max(0, (int) response.getWrittenRows()); // when statement alters schema no result rows returned. lastQueryId = response.getQueryId(); } catch (Exception e) { diff --git a/jdbc-v2/src/test/java/com/clickhouse/jdbc/PreparedStatementTest.java b/jdbc-v2/src/test/java/com/clickhouse/jdbc/PreparedStatementTest.java index d5bfb7978..0c7097bd4 100644 --- a/jdbc-v2/src/test/java/com/clickhouse/jdbc/PreparedStatementTest.java +++ b/jdbc-v2/src/test/java/com/clickhouse/jdbc/PreparedStatementTest.java @@ -1,5 +1,6 @@ package com.clickhouse.jdbc; +import com.clickhouse.client.api.ClientConfigProperties; import com.clickhouse.client.api.DataTypeUtils; import com.clickhouse.data.ClickHouseColumn; import com.clickhouse.data.ClickHouseDataType; @@ -566,6 +567,16 @@ void testSelectFromArray() throws Exception { } } + @Test(groups = {"integration"}) + void testExecuteQueryTimeout() throws Exception { + final String sql = "SELECT sum(reinterpretAsUInt64(MD5(toString(number)))) FROM system.numbers LIMIT 1000000"; + try (Connection conn = getJdbcConnection(); + PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setQueryTimeout(1); + assertThrows(SQLException.class, stmt::executeQuery); + } + } + @Test(groups = { "integration" }) void testInsert() throws Exception { int ROWS = 1000; diff --git a/jdbc-v2/src/test/java/com/clickhouse/jdbc/StatementTest.java b/jdbc-v2/src/test/java/com/clickhouse/jdbc/StatementTest.java index 575dde388..d54769b33 100644 --- a/jdbc-v2/src/test/java/com/clickhouse/jdbc/StatementTest.java +++ b/jdbc-v2/src/test/java/com/clickhouse/jdbc/StatementTest.java @@ -1034,7 +1034,7 @@ public void testUpdateQueryWithResultSet() throws Exception { props.setProperty(ClientConfigProperties.HTTP_MAX_OPEN_CONNECTIONS.getKey(), "1"); props.setProperty(ClientConfigProperties.CONNECTION_REQUEST_TIMEOUT.getKey(), "500"); try (Connection conn = getJdbcConnection(props); Statement stmt = conn.createStatement()) { - stmt.setQueryTimeout(1); + stmt.setQueryTimeout(10); ResultSet rs = stmt.executeQuery("SELECT 1"); boolean failedOnTimeout = false; try {