Skip to content

Commit 99dd5c6

Browse files
committed
refactor close
1 parent 41e00f3 commit 99dd5c6

1 file changed

Lines changed: 57 additions & 24 deletions

File tree

java-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryConnection.java

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -942,54 +942,84 @@ public void close() throws SQLException {
942942
}
943943

944944
private void closeImpl() throws SQLException {
945+
SQLException exceptionToThrow = null;
945946
try {
946947
if (this.bigQueryReadClient != null) {
947948
this.bigQueryReadClient.shutdown();
948-
this.bigQueryReadClient.awaitTermination(1, TimeUnit.MINUTES);
949-
this.bigQueryReadClient.close();
950949
}
951-
952950
if (this.bigQueryWriteClient != null) {
953951
this.bigQueryWriteClient.shutdown();
954-
this.bigQueryWriteClient.awaitTermination(1, TimeUnit.MINUTES);
955-
this.bigQueryWriteClient.close();
956952
}
957-
958-
for (Statement statement : this.openStatements) {
959-
statement.close();
960-
}
961-
this.openStatements.clear();
962-
963953
if (this.metadataExecutor != null) {
964954
this.metadataExecutor.shutdown();
965955
}
966956
if (this.queryExecutor != null) {
967957
this.queryExecutor.shutdown();
968958
}
969959

960+
for (Statement statement : this.openStatements) {
961+
try {
962+
statement.close();
963+
} catch (SQLException e) {
964+
if (exceptionToThrow == null) {
965+
exceptionToThrow = e;
966+
} else {
967+
exceptionToThrow.addSuppressed(e);
968+
}
969+
}
970+
}
971+
this.openStatements.clear();
972+
970973
boolean interrupted = Thread.currentThread().isInterrupted();
971974

972-
if (this.metadataExecutor != null) {
973-
try {
975+
try {
976+
if (this.bigQueryReadClient != null) {
977+
if (interrupted) {
978+
this.bigQueryReadClient.shutdownNow();
979+
} else {
980+
this.bigQueryReadClient.awaitTermination(1, TimeUnit.MINUTES);
981+
}
982+
}
983+
if (this.bigQueryWriteClient != null) {
984+
if (interrupted) {
985+
this.bigQueryWriteClient.shutdownNow();
986+
} else {
987+
this.bigQueryWriteClient.awaitTermination(1, TimeUnit.MINUTES);
988+
}
989+
}
990+
if (this.metadataExecutor != null) {
974991
if (interrupted || !this.metadataExecutor.awaitTermination(10, TimeUnit.SECONDS)) {
975992
this.metadataExecutor.shutdownNow();
976993
}
977-
} catch (InterruptedException e) {
978-
this.metadataExecutor.shutdownNow();
979-
interrupted = true;
980-
Thread.currentThread().interrupt();
981994
}
982-
}
983-
984-
if (this.queryExecutor != null) {
985-
try {
995+
if (this.queryExecutor != null) {
986996
if (interrupted || !this.queryExecutor.awaitTermination(10, TimeUnit.SECONDS)) {
987997
this.queryExecutor.shutdownNow();
988998
}
989-
} catch (InterruptedException e) {
999+
}
1000+
} catch (InterruptedException e) {
1001+
interrupted = true;
1002+
if (this.bigQueryReadClient != null) {
1003+
this.bigQueryReadClient.shutdownNow();
1004+
}
1005+
if (this.bigQueryWriteClient != null) {
1006+
this.bigQueryWriteClient.shutdownNow();
1007+
}
1008+
if (this.metadataExecutor != null) {
1009+
this.metadataExecutor.shutdownNow();
1010+
}
1011+
if (this.queryExecutor != null) {
9901012
this.queryExecutor.shutdownNow();
991-
interrupted = true;
992-
Thread.currentThread().interrupt();
1013+
}
1014+
} finally {
1015+
try {
1016+
if (this.bigQueryReadClient != null) {
1017+
this.bigQueryReadClient.close();
1018+
}
1019+
} finally {
1020+
if (this.bigQueryWriteClient != null) {
1021+
this.bigQueryWriteClient.close();
1022+
}
9931023
}
9941024
}
9951025

@@ -1005,6 +1035,9 @@ private void closeImpl() throws SQLException {
10051035
BigQueryJdbcMdc.clear();
10061036
BigQueryJdbcRootLogger.closeConnectionHandler(this.connectionId);
10071037
}
1038+
if (exceptionToThrow != null) {
1039+
throw exceptionToThrow;
1040+
}
10081041
this.isClosed = true;
10091042
}
10101043

0 commit comments

Comments
 (0)