clickhouse-java icon indicating copy to clipboard operation
clickhouse-java copied to clipboard

getDatabaseMajorVersion throws SQLException: "Failed to retrieve server version."

Open jeloba opened this issue 9 months ago • 1 comments

The database metadata object is throwing exceptions when fetching the server version.

Steps to reproduce

  1. Spin up a fresh Clickhouse test container
  2. Obtain a database connection to it
  3. call connection.getMetadata().getDatabaseMajorVersion()

Expected behaviour

The version should be returned. Using the connection object to fetch version() using a prepared statement works fine.

Code example

    container =
        new ClickHouseContainer("clickhouse/clickhouse-server:22.2");
    container.start();

    try (var c = container.createConnection("?")) {
      // executing the version fetch like this works
      var r = c.prepareStatement("SELECT version() as server_version").executeQuery();
      r.next();
      var v = r.getString("server_version");
      System.out.println(v);

      // this fails
      var v2 = c.getMetaData().getDatabaseMajorVersion();
      System.out.println(v2);
    }

Error log

Caused by: java.sql.SQLException: Failed to retrieve server version.
        at com.clickhouse.jdbc.ConnectionImpl.lambda$getServerVersion$0(ConnectionImpl.java:103)
        at java.base/java.util.Optional.orElseThrow(Optional.java:403)
        at com.clickhouse.jdbc.ConnectionImpl.getServerVersion(ConnectionImpl.java:103)
        at com.clickhouse.jdbc.metadata.DatabaseMetaData.getDatabaseMajorVersion(DatabaseMetaData.java:1188)

Configuration

Environment

  • Client version: jdbc 2
  • Language version: java 21
  • OS: linux

ClickHouse server

  • ClickHouse Server version: 22.2.3.1

jeloba avatar Mar 18 '25 12:03 jeloba

I'm interested in fixing this bug too. It's been broken since 0.8.0 version. Works fine with 0.7.2

E1izabeth avatar May 02 '25 13:05 E1izabeth

Good @jeloba, @E1izabeth ! What client version do you use? Is there something special with your ClickHouse setup? Exceptions only tells that something went wrong - no detail. Do you have a complete stack trace?

I've tested with 0.9.0 version and it works.

Thanks in advance!

chernser avatar Jun 30 '25 21:06 chernser

Hi, @chernser I just retested, the results are:

  • the bug is not reproducible for ClickHouse 24.5.3.5, it's actual only for older versions (ClickHouse 22.1.3.7 in my case)
  • it's reproducible for all driver versions starting from 0.8.0, including 0.9.0; the last working version was 0.7.2

E1izabeth avatar Jul 01 '25 11:07 E1izabeth

@E1izabeth Thank you for the information! I will retest today with older version.

chernser avatar Jul 01 '25 14:07 chernser