Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think it would be great to just build up a single list of AutoCloseables that we can pass to AutoCloseables so that an exception in one doesn't skip the rest, and hopefully that will clean up the logic too.

Copy link
Contributor Author

@ennuite ennuite Jan 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't build a single list of AutoCloseables because ExecutorService isn't AutoCloseable before Java 19. I built a list with the ones I could, and left the try catch logic for the rest. Is this ok?

A question that hit me while doing this was what MT-safety guarantees we have on the implementation of the JDBC driver. The JDBC 4.3 spec doesn't speak about whether implementations of interfaces such as Connection need to be MT-safe, but older versions of the spec do speak about it: https://web.archive.org/web/20090213013250/http://java.sun.com/j2se/1.3/docs/guide/jdbc/spec/jdbc-spec.frame9.html

It seems to me like we are assuming each Connection is only used in a single thread, is this documented somewhere?

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import io.netty.util.concurrent.DefaultThreadFactory;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -180,19 +181,39 @@ public Properties getClientInfo() {

@Override
public void close() throws SQLException {
clientHandler.close();
if (executorService != null) {
executorService.shutdown();
Exception topLevelException = null;
try {
if (executorService != null) {
executorService.shutdown();
}
} catch (final Exception e) {
topLevelException = e;
}
ArrayList<AutoCloseable> closeables = new ArrayList<>(statementMap.values());
closeables.add(clientHandler);
closeables.addAll(allocator.getChildAllocators());
closeables.add(allocator);
try {
AutoCloseables.close(closeables);
} catch (final Exception e) {
if (topLevelException == null) {
topLevelException = e;
} else {
topLevelException.addSuppressed(e);
}
}

try {
AutoCloseables.close(clientHandler);
allocator.getChildAllocators().forEach(AutoCloseables::closeNoChecked);
AutoCloseables.close(allocator);

super.close();
} catch (final Exception e) {
throw AvaticaConnection.HELPER.createException(e.getMessage(), e);
if (topLevelException == null) {
topLevelException = e;
} else {
topLevelException.addSuppressed(e);
}
}
if (topLevelException != null) {
throw AvaticaConnection.HELPER.createException(
topLevelException.getMessage(), topLevelException);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.arrow.driver.jdbc;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -27,6 +28,7 @@
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;
import java.util.Properties;
import org.apache.arrow.driver.jdbc.authentication.UserPasswordAuthentication;
Expand Down Expand Up @@ -660,4 +662,40 @@ public String visit(String value) {
assertEquals(catalog, actualCatalog);
}
}

@Test
public void testStatementsClosedOnConnectionClose() throws Exception {
// create a connection
final Properties properties = new Properties();
properties.put(ArrowFlightConnectionProperty.HOST.camelName(), "localhost");
properties.put(
ArrowFlightConnectionProperty.PORT.camelName(), FLIGHT_SERVER_TEST_EXTENSION.getPort());
properties.put(ArrowFlightConnectionProperty.USER.camelName(), userTest);
properties.put(ArrowFlightConnectionProperty.PASSWORD.camelName(), passTest);
properties.put("useEncryption", false);

Connection connection =
DriverManager.getConnection(
"jdbc:arrow-flight-sql://"
+ FLIGHT_SERVER_TEST_EXTENSION.getHost()
+ ":"
+ FLIGHT_SERVER_TEST_EXTENSION.getPort(),
properties);

// create some statements
int numStatements = 3;
Statement[] statements = new Statement[numStatements];
for (int i = 0; i < numStatements; i++) {
statements[i] = connection.createStatement();
assertFalse(statements[i].isClosed());
}

// close the connection
connection.close();

// assert the statements are closed
for (int i = 0; i < numStatements; i++) {
assertTrue(statements[i].isClosed());
}
}
}