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.

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.List;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -180,19 +181,30 @@ public Properties getClientInfo() {

@Override
public void close() throws SQLException {
clientHandler.close();
if (executorService != null) {
executorService.shutdown();
Exception topLevelException = null;
try {
AutoCloseables.close(List.copyOf(statementMap.values()));
} catch (final Exception e) {
topLevelException = e;
}

try {
AutoCloseables.close(clientHandler);
if (executorService != null) {
executorService.shutdown();
}
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 @@ -16,6 +16,7 @@
*/
package org.apache.arrow.driver.jdbc;

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 @@ -26,6 +27,7 @@
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import org.apache.arrow.driver.jdbc.authentication.UserPasswordAuthentication;
import org.apache.arrow.driver.jdbc.client.ArrowFlightSqlClientHandler;
Expand Down Expand Up @@ -622,4 +624,40 @@ public void testJdbcDriverVersionIntegration() throws Exception {
"Expected: " + expectedUserAgent + " but found: " + actualUserAgent);
}
}

@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());
}
}
}
Loading