Skip to content

Commit 318e7e1

Browse files
committed
Moved loading server context to a public method
1 parent 7a15ecb commit 318e7e1

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

client-v2/src/main/java/com/clickhouse/client/api/Client.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,10 @@
4545
import com.clickhouse.client.config.ClickHouseClientOption;
4646
import com.clickhouse.data.ClickHouseColumn;
4747
import com.clickhouse.data.ClickHouseFormat;
48-
import org.apache.hc.client5.http.ConnectTimeoutException;
4948
import org.apache.hc.core5.concurrent.DefaultThreadFactory;
5049
import org.apache.hc.core5.http.ClassicHttpResponse;
51-
import org.apache.hc.core5.http.ConnectionRequestTimeoutException;
5250
import org.apache.hc.core5.http.HttpHeaders;
5351
import org.apache.hc.core5.http.HttpStatus;
54-
import org.apache.hc.core5.http.NoHttpResponseException;
5552
import org.slf4j.Logger;
5653
import org.slf4j.LoggerFactory;
5754

@@ -62,8 +59,6 @@
6259
import java.io.OutputStream;
6360
import java.lang.reflect.InvocationTargetException;
6461
import java.lang.reflect.Method;
65-
import java.net.ConnectException;
66-
import java.net.SocketTimeoutException;
6762
import java.net.URL;
6863
import java.nio.charset.StandardCharsets;
6964
import java.time.Duration;
@@ -77,7 +72,6 @@
7772
import java.util.LinkedHashMap;
7873
import java.util.List;
7974
import java.util.Map;
80-
import java.util.Properties;
8175
import java.util.Set;
8276
import java.util.StringJoiner;
8377
import java.util.TimeZone;
@@ -185,12 +179,13 @@ private Client(Set<String> endpoints, Map<String,String> configuration, boolean
185179
LOG.info("Using old http client implementation");
186180
}
187181
this.columnToMethodMatchingStrategy = columnToMethodMatchingStrategy;
188-
189-
190-
updateServerContext();
191182
}
192183

193-
private void updateServerContext() {
184+
/**
185+
* Loads essential information about a server. Should be called after client creation.
186+
*
187+
*/
188+
public void updateServerContext() {
194189
try (QueryResponse response = this.query("SELECT currentUser() AS user, timezone() AS timezone, version() AS version LIMIT 1").get()) {
195190
try (ClickHouseBinaryFormatReader reader = this.newBinaryFormatReader(response)) {
196191
if (reader.next() != null) {
@@ -200,13 +195,10 @@ private void updateServerContext() {
200195
}
201196
}
202197
} catch (Exception e) {
203-
LOG.error("Failed to get server info", e);
198+
throw new ClientException("Failed to get server info", e);
204199
}
205200
}
206201

207-
208-
209-
210202
/**
211203
* Returns default database name that will be used by operations if not specified.
212204
*

client-v2/src/test/java/com/clickhouse/client/ClientTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,15 @@ public void testSetOptions() {
130130
}
131131
}
132132

133+
@Test
134+
public void testSkipServerLoadInfo() throws Exception {
135+
try (Client client = newClient().build()) {
136+
Assert.assertNull(client.getServerVersion());
137+
client.updateServerContext();
138+
Assert.assertNotNull(client.getServerVersion());
139+
}
140+
}
141+
133142
protected Client.Builder newClient() {
134143
ClickHouseNode node = getServer(ClickHouseProtocol.HTTP);
135144
boolean isSecure = isCloud();

jdbc-v2/src/main/java/com/clickhouse/jdbc/ConnectionImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public ConnectionImpl(String url, Properties info) throws SQLException {
7676
this.client = this.config.applyClientProperties(new Client.Builder())
7777
.setClientName(clientName)
7878
.build();
79+
this.client.updateServerContext();
7980
this.schema = client.getDefaultDatabase();
8081
this.defaultQuerySettings = new QuerySettings()
8182
.serverSetting(ServerSettings.ASYNC_INSERT, "0")

0 commit comments

Comments
 (0)