diff --git a/client-v2/src/main/java/com/clickhouse/client/api/Client.java b/client-v2/src/main/java/com/clickhouse/client/api/Client.java index 0d843d3c0..348567da1 100644 --- a/client-v2/src/main/java/com/clickhouse/client/api/Client.java +++ b/client-v2/src/main/java/com/clickhouse/client/api/Client.java @@ -258,7 +258,8 @@ public void loadServerInfo() { if (tmpDbUser != null && !tmpDbUser.isEmpty()) { this.dbUser = tmpDbUser; } - this.configuration.put(ClientConfigProperties.SERVER_TIMEZONE.getKey(), reader.getString("timezone")); + this.configuration.put(ClientConfigProperties.SERVER_TIMEZONE.getKey(), + ClientConfigProperties.SERVER_TIMEZONE.parseValue(reader.getString("timezone"))); serverVersion = reader.getString("version"); } } diff --git a/client-v2/src/test/java/com/clickhouse/client/ClientTests.java b/client-v2/src/test/java/com/clickhouse/client/ClientTests.java index 050e6cd0c..5f41dba90 100644 --- a/client-v2/src/test/java/com/clickhouse/client/ClientTests.java +++ b/client-v2/src/test/java/com/clickhouse/client/ClientTests.java @@ -322,6 +322,22 @@ public void testLoadingServerContext() throws Exception { } } + @Test(groups = {"integration"}) + public void testServerTimeZoneAfterLoadingServerInfo() throws Exception { + try (Client client = newClient().build()) { + // Before loadServerInfo() the client returns the configured value. + Assert.assertEquals(client.getServerTimeZone(), "UTC"); + + String expected = client.queryAll("SELECT timezone()").get(0).getString(1); + client.loadServerInfo(); + Assert.assertEquals(client.getServerTimeZone(), expected); + } + + try (Client client = newClient().setServerTimeZone("Europe/Berlin").build()) { + Assert.assertEquals(client.getServerTimeZone(), "Europe/Berlin"); + } + } + @Test(groups = {"integration"}) public void testDisableNative() { try (Client client = newClient().disableNativeCompression(true).build()) { diff --git a/history/latest/3357.md b/history/latest/3357.md new file mode 100644 index 000000000..7e1e1c53c --- /dev/null +++ b/history/latest/3357.md @@ -0,0 +1,4 @@ +- **[client-v2]** Fixed `Client#getServerTimeZone()`, which failed with `ClassCastException` (`String` cannot be cast + to `TimeZone`) after a call to `Client#loadServerInfo()`. `loadServerInfo()` now stores the server time zone as a + `TimeZone`, the same type as the parsed client configuration. + (https://github.com/ClickHouse/clickhouse-java/issues/3357)