Skip to content

Commit 227bb62

Browse files
author
Paultagoras
committed
Addressing PR feedback
1 parent f7ac19c commit 227bb62

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.clickhouse.client.api;
22

33
import com.clickhouse.client.*;
4+
import com.clickhouse.client.api.exception.ClientException;
45
import com.clickhouse.client.api.insert.InsertResponse;
56
import com.clickhouse.client.api.insert.InsertSettings;
67
import com.clickhouse.client.api.insert.POJOSerializer;
@@ -242,7 +243,7 @@ public void register(Class<?> clazz, TableSchema schema) {
242243
*/
243244
public InsertResponse insert(String tableName,
244245
List<Object> data,
245-
InsertSettings settings) throws IOException, InvocationTargetException, IllegalAccessException, ExecutionException, InterruptedException {
246+
InsertSettings settings) throws ClientException, IOException {
246247
if (data == null || data.isEmpty()) {
247248
throw new IllegalArgumentException("Data cannot be empty");
248249
}
@@ -274,7 +275,11 @@ public InsertResponse insert(String tableName,
274275
//Call the static .serialize method on the POJOSerializer for each object in the list
275276
for (Object obj : data) {
276277
for (POJOSerializer serializer : serializers) {
277-
serializer.serialize(obj, stream);
278+
try {
279+
serializer.serialize(obj, stream);
280+
} catch (InvocationTargetException | IllegalAccessException | IOException e) {
281+
throw new ClientException(e);
282+
}
278283
}
279284
}
280285
long s3 = System.currentTimeMillis();
@@ -288,7 +293,7 @@ public InsertResponse insert(String tableName,
288293
*/
289294
public InsertResponse insert(String tableName,
290295
InputStream data,
291-
InsertSettings settings) throws IOException, ExecutionException, InterruptedException {
296+
InsertSettings settings) throws IOException, ClientException {
292297
long s1 = System.currentTimeMillis();
293298
InsertResponse response;
294299
try (ClickHouseClient client = createClient()) {
@@ -306,7 +311,11 @@ public InsertResponse insert(String tableName,
306311
stream.write(buffer, 0, bytesRead);
307312
}
308313
}
309-
response = new InsertResponse(client, future.get());
314+
try {
315+
response = new InsertResponse(client, future.get());
316+
} catch (InterruptedException | ExecutionException e) {
317+
throw new ClientException("Operation has likely timed out.", e);
318+
}
310319
}
311320

312321
long s2 = System.currentTimeMillis();
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.clickhouse.client.api.exception;
2+
3+
public class ClientException extends Throwable{
4+
public ClientException() {
5+
super();
6+
}
7+
8+
public ClientException(Throwable cause) {
9+
super(cause);
10+
}
11+
12+
public ClientException(String message) {
13+
super(message);
14+
}
15+
16+
public ClientException(String message, Throwable cause) {
17+
super(message, cause);
18+
}
19+
}

client-v2/src/main/java/com/clickhouse/client/api/insert/InsertResponse.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ public ClickHouseResponseSummary getSummary() {
1919

2020
@Override
2121
public void close() {
22-
responseRef.close();
23-
client.close();
22+
try {
23+
responseRef.close();
24+
} finally {
25+
client.close();
26+
}
2427
}
2528
}

client-v2/src/test/java/com/clickhouse/client/insert/InsertTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.clickhouse.client.*;
44
import com.clickhouse.client.api.Protocol;
5+
import com.clickhouse.client.api.exception.ClientException;
56
import com.clickhouse.client.api.insert.InsertResponse;
67
import com.clickhouse.client.api.insert.InsertSettings;
78
import com.clickhouse.client.api.Client;
@@ -10,9 +11,7 @@
1011
import org.testng.annotations.Test;
1112

1213
import java.io.IOException;
13-
import java.lang.reflect.InvocationTargetException;
1414
import java.util.*;
15-
import java.util.concurrent.ExecutionException;
1615

1716
import static org.testng.Assert.*;
1817

@@ -42,7 +41,7 @@ private void createTable(String tableQuery) throws ClickHouseException {
4241
}
4342

4443
@Test(groups = { "unit" }, enabled = true)
45-
public void insertSimplePOJOs() throws ClickHouseException, IOException, ExecutionException, InterruptedException, InvocationTargetException, IllegalAccessException {
44+
public void insertSimplePOJOs() throws ClickHouseException, ClientException, IOException {
4645
String tableName = "simple_pojo_table";
4746
String createSQL = SamplePOJO.generateTableCreateSQL(tableName);
4847
System.out.println(createSQL);

0 commit comments

Comments
 (0)