Skip to content

Commit 08186a8

Browse files
committed
Refactor: Ensure all resources are closed when exceptions are thrown
1 parent 5eb9998 commit 08186a8

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/ArrowFlightConnection.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import io.netty.util.concurrent.DefaultThreadFactory;
2222
import java.sql.SQLException;
23-
import java.util.List;
23+
import java.util.ArrayList;
2424
import java.util.Properties;
2525
import java.util.concurrent.ExecutorService;
2626
import java.util.concurrent.Executors;
@@ -183,17 +183,26 @@ public Properties getClientInfo() {
183183
public void close() throws SQLException {
184184
Exception topLevelException = null;
185185
try {
186-
AutoCloseables.close(List.copyOf(statementMap.values()));
186+
if (executorService != null) {
187+
executorService.shutdown();
188+
}
187189
} catch (final Exception e) {
188190
topLevelException = e;
189191
}
192+
ArrayList<AutoCloseable> closeables = new ArrayList<>(statementMap.values());
193+
closeables.add(clientHandler);
194+
closeables.addAll(allocator.getChildAllocators());
195+
closeables.add(allocator);
190196
try {
191-
AutoCloseables.close(clientHandler);
192-
if (executorService != null) {
193-
executorService.shutdown();
197+
AutoCloseables.close(closeables);
198+
} catch (final Exception e) {
199+
if (topLevelException == null) {
200+
topLevelException = e;
201+
} else {
202+
topLevelException.addSuppressed(e);
194203
}
195-
allocator.getChildAllocators().forEach(AutoCloseables::closeNoChecked);
196-
AutoCloseables.close(allocator);
204+
}
205+
try {
197206
super.close();
198207
} catch (final Exception e) {
199208
if (topLevelException == null) {

0 commit comments

Comments
 (0)