Skip to content

Commit 9f73d22

Browse files
committed
Fix memory leak
1 parent 9ce07fc commit 9f73d22

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import io.netty.util.concurrent.DefaultThreadFactory;
2222
import java.sql.SQLException;
2323
import java.util.ArrayList;
24+
import java.util.HashMap;
25+
import java.util.Map;
2426
import java.util.Properties;
2527
import java.util.concurrent.ExecutorService;
2628
import java.util.concurrent.Executors;
@@ -42,6 +44,8 @@ public final class ArrowFlightConnection extends AvaticaConnection {
4244
private final ArrowFlightSqlClientHandler clientHandler;
4345
private final ArrowFlightConnectionConfigImpl config;
4446
private ExecutorService executorService;
47+
int metadataResultSetCount;
48+
Map<Integer, ArrowFlightJdbcFlightStreamResultSet> metadataResultSetMap = new HashMap<>();
4549

4650
/**
4751
* Creates a new {@link ArrowFlightConnection}.
@@ -66,6 +70,7 @@ private ArrowFlightConnection(
6670
this.config = Preconditions.checkNotNull(config, "Config cannot be null.");
6771
this.allocator = Preconditions.checkNotNull(allocator, "Allocator cannot be null.");
6872
this.clientHandler = Preconditions.checkNotNull(clientHandler, "Handler cannot be null.");
73+
this.metadataResultSetCount = 0;
6974
}
7075

7176
/**
@@ -190,7 +195,9 @@ public void close() throws SQLException {
190195
} catch (final Exception e) {
191196
topLevelException = e;
192197
}
198+
// copies of the collections are used to avoid concurrent modification problems
193199
ArrayList<AutoCloseable> closeables = new ArrayList<>(statementMap.values());
200+
closeables.addAll(new ArrayList<>(metadataResultSetMap.values()));
194201
closeables.add(clientHandler);
195202
closeables.addAll(allocator.getChildAllocators());
196203
closeables.add(allocator);

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public final class ArrowFlightJdbcFlightStreamResultSet
5454
private VectorSchemaRoot currentVectorSchemaRoot;
5555

5656
private Schema schema;
57+
private Integer id = null; // used for metadata result sets only
5758

5859
/** Public constructor used by ArrowFlightJdbcFactory. */
5960
ArrowFlightJdbcFlightStreamResultSet(
@@ -82,6 +83,8 @@ private ArrowFlightJdbcFlightStreamResultSet(
8283
super(null, state, signature, resultSetMetaData, timeZone, firstFrame);
8384
this.connection = connection;
8485
this.flightInfo = flightInfo;
86+
this.id = connection.metadataResultSetCount++;
87+
connection.metadataResultSetMap.put(id, this);
8588
}
8689

8790
/**
@@ -234,7 +237,14 @@ protected void cancel() {
234237

235238
@Override
236239
public synchronized void close() {
240+
237241
try {
242+
if (isClosed()) {
243+
return;
244+
}
245+
if (id != null) { // id is only set for metadata result sets
246+
this.connection.metadataResultSetMap.remove(id);
247+
}
238248
if (flightEndpointDataQueue != null) {
239249
// flightStreamQueue should close currentFlightStream internally
240250
flightEndpointDataQueue.close();

0 commit comments

Comments
 (0)