Skip to content

Commit 344d751

Browse files
committed
Fixed overriding server settings in jdbc connection
1 parent cd9b60d commit 344d751

5 files changed

Lines changed: 83 additions & 7 deletions

File tree

client-v2/src/main/java/com/clickhouse/client/api/internal/ServerSettings.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,9 @@ public final class ServerSettings {
4646
public static final String ASYNC_INSERT = "async_insert";
4747

4848
public static final String WAIT_ASYNC_INSERT = "wait_for_async_insert";
49+
50+
// Misc
51+
public static final String ON = "1";
52+
53+
public static final String OFF = "0";
4954
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,7 @@ public ConnectionImpl(String url, Properties info) throws SQLException {
110110
this.client.loadServerInfo();
111111
}
112112
this.schema = client.getDefaultDatabase();
113-
this.defaultQuerySettings = new QuerySettings()
114-
.serverSetting(ServerSettings.ASYNC_INSERT, "0")
115-
.serverSetting(ServerSettings.WAIT_END_OF_QUERY, "0");
113+
this.defaultQuerySettings = new QuerySettings();
116114

117115
this.metadata = new DatabaseMetaDataImpl(this, false, url);
118116
this.defaultCalendar = Calendar.getInstance();

jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/JdbcConfiguration.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.clickhouse.client.api.Client;
44
import com.clickhouse.client.api.ClientConfigProperties;
55
import com.clickhouse.client.api.http.ClickHouseHttpProto;
6+
import com.clickhouse.client.api.internal.ServerSettings;
67
import com.clickhouse.data.ClickHouseDataType;
78
import com.clickhouse.jdbc.Driver;
89
import com.clickhouse.jdbc.DriverProperties;
@@ -89,7 +90,7 @@ public JdbcConfiguration(String url, Properties info) throws SQLException {
8990

9091
Map<String, String> urlProperties = parseUrl(url);
9192
String tmpConnectionUrl = urlProperties.remove(PARSE_URL_CONN_URL_PROP);
92-
initProperties(urlProperties, props);
93+
buildFinalProperties(urlProperties, props);
9394

9495
// after initializing all properties - set final connection URL
9596
boolean useSSLInfo = Boolean.parseBoolean(props.getProperty(DriverProperties.SECURE_CONNECTION.getKey(), "false"));
@@ -266,10 +267,31 @@ private Map<String, String> parseUrl(String url) throws SQLException {
266267
return properties;
267268
}
268269

269-
private void initProperties(Map<String, String> urlProperties, Properties providedProperties) {
270+
/**
271+
* Creates initial properties with defaults.
272+
* @return mutable HashMap with default values.
273+
*/
274+
Map<String, String> createProperties() {
275+
Map<String, String> props = new HashMap<>();
276+
277+
// Requires to wait result on insert
278+
props.put(DriverProperties.serverSetting(ServerSettings.ASYNC_INSERT), ServerSettings.OFF);
279+
280+
// Requires to wait result of query (manly insert)
281+
props.put(DriverProperties.serverSetting(ServerSettings.WAIT_END_OF_QUERY), ServerSettings.ON);
282+
283+
return props;
284+
}
285+
286+
/**
287+
* Combines url properties and provided ones via {@link java.sql.Driver#connect(String, Properties)}
288+
* @param urlProperties - properties parsed from URL
289+
* @param providedProperties - properties object provided by application
290+
*/
291+
private void buildFinalProperties(Map<String, String> urlProperties, Properties providedProperties) {
270292

271293
// Copy provided properties
272-
Map<String, String> props = new HashMap<>();
294+
Map<String, String> props = createProperties();
273295
// Set driver properties defaults (client will do the same)
274296
for (DriverProperties prop : DriverProperties.values()) {
275297
if (prop.getDefaultValue() != null) {

jdbc-v2/src/test/java/com/clickhouse/jdbc/StatementTest.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,54 @@ public void testExecuteUpdateDates() throws Exception {
264264
}
265265
}
266266

267+
@DataProvider(name = "asyncInsertSettingsDP")
268+
public static Object[][] asyncInsertSettingsDP() {
269+
return new Object[][]{
270+
// asyncInsert, waitEndOfQuery, expectedUpdateCount, expectedSelectCount
271+
{ServerSettings.OFF, ServerSettings.OFF, 10000},
272+
{ServerSettings.OFF, ServerSettings.ON, 10000},
273+
{ServerSettings.ON, ServerSettings.OFF, 0, -1},
274+
{ServerSettings.ON, ServerSettings.ON, 0, 10000}
275+
};
276+
}
277+
278+
@Test(groups = {"integration"}, dataProvider = "asyncInsertSettingsDP")
279+
public void testInsertWithAsyncInsert(String asyncInsert, String waitAsyncInsert, int expectedUpdateCount, int expectedSelectCount) throws Exception {
280+
String tableName = "test_async_insert_param_" + asyncInsert + "_" + waitAsyncInsert;
281+
282+
Properties props = new Properties();
283+
props.setProperty(ClientConfigProperties.serverSetting(ServerSettings.ASYNC_INSERT), asyncInsert);
284+
props.setProperty(ClientConfigProperties.serverSetting(ServerSettings.WAIT_ASYNC_INSERT), waitAsyncInsert);
285+
// Wait end of query off for isolation of this logic
286+
props.setProperty(ClientConfigProperties.serverSetting(ServerSettings.WAIT_END_OF_QUERY), ServerSettings.OFF);
287+
288+
try (Connection conn = getJdbcConnection(props)) {
289+
try (Statement stmt = conn.createStatement()) {
290+
stmt.execute("CREATE TABLE IF NOT EXISTS " + getDatabase() + "." + tableName + " (id UInt32) ENGINE = MergeTree ORDER BY id");
291+
stmt.execute("TRUNCATE TABLE " + getDatabase() + "." + tableName);
292+
293+
StringBuilder sb = new StringBuilder("INSERT INTO " + getDatabase() + "." + tableName + " VALUES ");
294+
for (int i = 0; i < 10000; i++) {
295+
if (i > 0) sb.append(", ");
296+
sb.append("(").append(i).append(")");
297+
}
298+
299+
int updateCount = stmt.executeUpdate(sb.toString());
300+
assertEquals(updateCount, expectedUpdateCount);
301+
302+
try (ResultSet rs = stmt.executeQuery("SELECT count() FROM " + getDatabase() + "." + tableName)) {
303+
assertTrue(rs.next());
304+
int count = rs.getInt(1);
305+
if (expectedSelectCount == -1) {
306+
assertTrue(count < 10000, "Expected count to be < 10000, but was: " + count);
307+
} else {
308+
assertEquals(count, expectedSelectCount);
309+
}
310+
}
311+
}
312+
}
313+
}
314+
267315

268316
@Test(groups = {"integration"})
269317
public void testExecuteUpdateBatch() throws Exception {

jdbc-v2/src/test/java/com/clickhouse/jdbc/internal/JdbcConfigurationTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,11 @@ public void testParseURLValid(String jdbcURL, Properties properties,
121121
throws Exception
122122
{
123123
JdbcConfiguration configuration = new JdbcConfiguration(jdbcURL, properties);
124+
// We need to copy defaults to expected.
125+
Map<String, String> expectedWithDefaults = configuration.createProperties();
126+
expectedWithDefaults.putAll(expectedClientProps);
124127
assertEquals(configuration.getConnectionUrl(), connectionURL, "URL: " + jdbcURL);
125-
assertEquals(configuration.clientProperties, expectedClientProps, "URL: " + jdbcURL);
128+
assertEquals(configuration.clientProperties, expectedWithDefaults, "URL: " + jdbcURL);
126129
Client.Builder bob = new Client.Builder();
127130
configuration.applyClientProperties(bob);
128131
Client client = bob.build();

0 commit comments

Comments
 (0)