Skip to content

[jdbc-v2] Handle query timeout in the middle of stream #3077

In review

Description

@chernser

Description

Query may have execution timeout limit on server. In this case exception will be just SQLException not a SQLTimeoutException.
JDBC do not make driver throw SQLTimeoutException in this case but it would be good to fix for consistency and better error handling.

Requires: #2702

Activity

  1. added
    jdbc
    on Aug 25, 2026
  2. jjj-n commented on Sep 1, 2026

    @jjj-n
    Contributor

    Hi @chernser, I'd like to work on this issue.

    My plan is to trace how a server-side execution timeout propagates through jdbc-v2 after the response stream has already been opened, and add a regression test where ClickHouse error 159 is received while consuming the ResultSet.

    I will then update the JDBC exception mapping so this path throws SQLTimeoutException while preserving the original cause and vendor error code. I will also verify whether synchronous and asynchronous operations share this path and cover both where applicable.

    Does this scope match what you expect for #3077?

  3. chernser commented on Sep 1, 2026

    @chernser
    ContributorAuthor

    Good day, @jjj-n !

    Sure you can take it!

    • You need to resolve [client-v2] Handle new error tag from ClickHouse #2702 - Go client should have reference implementation
    • We need verification test in client-v2 to record in tests that we handle timeout while reading stream correctly (may be specific exception would be needed because we want to add timeout to fault cause)
    • JDBC the main fix - just another test and proper exception wrapping/handling

    Thanks!

  4. added this to the 0.11.0 milestone on Sep 9, 2026
  5. modified the milestones: 0.11.0, 0.12.0 on Sep 23, 2026
  6. removed this from the 0.12.0 milestone on Oct 14, 2026
  7. polyglotAI-bot commented on Nov 27, 2026

    @polyglotAI-bot
    Collaborator

    Recommendation: Keep open — fix needed
    A server timeout after the first rows loses error 159 completely, not only the exception type. Open PR #3097 fixes it at its own head (SQLTimeoutException, HYT00, code 159), but it does not apply to main, its base cannot read the compressed responses of server 26.11, and the maintainer paused it on 2026-09-24.

    Analysis: On main, client-v2 does not read the error that the server appends to an HTTP 200 response (#2702). The reader gets a truncated chunked body and throws a generic read error (BinaryStreamReader.java:307). StatementImpl.java:378 maps error 159 to SQLTimeoutException only when it arrives before the rows.

    Reproduction: complete program and output
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Properties;
    
    public class Repro {
        static final String URL = System.getenv().getOrDefault("CLICKHOUSE_URL", "http://localhost:8123");
        static final String PASSWORD = System.getenv().getOrDefault("CLICKHOUSE_PASSWORD", "");
        // About 1 MB of rows each 0.2 s: rows arrive before the server stops the query after 1 s.
        static final String STREAMING = "SELECT number, randomPrintableASCII(1000) AS s, sleepEachRow(0.0002)"
                + " FROM numbers(100000) SETTINGS max_block_size = 1000";
        // No row arrives before the timeout.
        static final String AGGREGATE = "SELECT sum(sleepEachRow(0.01)) FROM numbers(1000) SETTINGS max_block_size = 10";
    
        static void runCase(Connection conn, String name, String sql, boolean queryTimeout) {
            int rows = 0;
            String stage = "executeQuery";
            try (Statement st = conn.createStatement()) {
                if (queryTimeout) st.setQueryTimeout(1);
                String q = queryTimeout ? sql : sql + ", max_execution_time = 1";
                try (ResultSet rs = st.executeQuery(q)) {
                    stage = "next";
                    while (rs.next()) rows++;
                }
                System.out.println(name + ": no error, rows=" + rows);
            } catch (SQLException e) {
                String msg = String.valueOf(e.getMessage());
                System.out.println(name + ": " + stage + " after " + rows + " rows: " + e.getClass().getName()
                        + " (SQLState " + e.getSQLState() + ", code " + e.getErrorCode() + "): "
                        + (msg.length() > 90 ? msg.substring(0, 90) + "…" : msg));
                for (Throwable c = e.getCause(); c != null; c = c.getCause()) {
                    String m = String.valueOf(c.getMessage());
                    System.out.println("    caused by " + c.getClass().getName() + ": " + (m.length() > 90 ? m.substring(0, 90) + "…" : m));
                }
            }
        }
    
        public static void main(String[] args) throws Exception {
            Properties props = new Properties();
            props.setProperty("user", "default");
            props.setProperty("password", PASSWORD);
            String jdbcUrl = "jdbc:clickhouse:" + URL.replaceFirst("^https?:", "") + "/default";
            try (Connection conn = DriverManager.getConnection(jdbcUrl, props)) {
                runCase(conn, "setQueryTimeout(1), timeout mid-stream    ", STREAMING, true);
                runCase(conn, "max_execution_time=1, timeout mid-stream  ", STREAMING, false);
                runCase(conn, "setQueryTimeout(1), timeout before stream ", AGGREGATE, true);
            }
        }
    }
    

    Output on main (8480ca001), ClickHouse 26.11.2.4:

    setQueryTimeout(1), timeout mid-stream    : next after 4000 rows: java.sql.SQLException (SQLState HY000, code 0): Failed to read value for column s
        caused by com.clickhouse.client.api.ClientException: Failed to read value for column s
        caused by org.apache.hc.core5.http.ConnectionClosedException: Premature end of chunk coded message body: closing chunk expected
    max_execution_time=1, timeout mid-stream  : next after 4000 rows: java.sql.SQLException (SQLState HY000, code 0): Failed to read value for column s
        caused by com.clickhouse.client.api.ClientException: Failed to read value for column s
        caused by org.apache.hc.core5.http.ConnectionClosedException: Premature end of chunk coded message body: closing chunk expected
    setQueryTimeout(1), timeout before stream : executeQuery after 0 rows: java.sql.SQLTimeoutException (SQLState null, code 0): Query execution time exceeded limit (queryId=…)
        caused by com.clickhouse.client.api.ServerException: Code: 159. DB::Exception: Timeout exceeded: elapsed 1002.399 ms, maximum: 1000.000 ms. (TI…
    

    Output with PR #3097 at its head (7429ce0a0). This run adds compress=false to the properties, because the PR base cannot read the compressed responses of server 26.11. The PR base is older than #3147, so setQueryTimeout sets no server limit there, and only the max_execution_time case applies:

    setQueryTimeout(1), timeout mid-stream    : no error, rows=100000
    max_execution_time=1, timeout mid-stream  : next after 4000 rows: java.sql.SQLTimeoutException (SQLState HYT00, code 159): Code: 159. DB::Exception: Timeout exceeded: elapsed 1003.858 ms, maximum: 1000.000 ms. (TI…
    setQueryTimeout(1), timeout before stream : no error, rows=1
    

    Automated triage by Polyglot AI. A maintainer makes the final decision.

  8. added
    api-diagnostics
    client
    on Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-diagnosticsbugclientjdbc

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions