Skip to content

feat(java/driver/jni): close child resources automatically#4441

Merged
lidavidm merged 3 commits into
apache:mainfrom
lidavidm:jni-hierarchy
Jun 25, 2026
Merged

feat(java/driver/jni): close child resources automatically#4441
lidavidm merged 3 commits into
apache:mainfrom
lidavidm:jni-hierarchy

Conversation

@lidavidm

@lidavidm lidavidm commented Jun 24, 2026

Copy link
Copy Markdown
Member

This mimics what the Python driver manager does. Unlike Python, it simply closes the children. It does not protect against concurrent usage.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR experiments with enforcing parent/child ADBC resource lifetimes in the Java JNI driver (similar to the Python driver manager), so that closing a connection also closes any statements/readers derived from it and prevents use-after-close.

Changes:

  • Add OwnedReferences to track and close child resources when the parent connection closes.
  • Tie ArrowReader lifetimes to the producing connection/statement via TiedArrowReader and updated importStream(...) plumbing.
  • Add a regression test ensuring statements become unusable after their connection is closed.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
java/driver/jni/src/test/java/org/apache/arrow/adbc/driver/jni/JniDriverTest.java Adds a test asserting connection close invalidates/cleans up statements.
java/driver/jni/src/main/java/org/apache/arrow/adbc/driver/jni/JniStatement.java Registers statements as owned by the parent connection and ties query readers to the statement lifetime.
java/driver/jni/src/main/java/org/apache/arrow/adbc/driver/jni/JniConnection.java Tracks statements/readers created from the connection and closes them on connection close.
java/driver/jni/src/main/java/org/apache/arrow/adbc/driver/jni/impl/TiedArrowReader.java New delegating ArrowReader that retains a parent handle to keep it reachable while reading.
java/driver/jni/src/main/java/org/apache/arrow/adbc/driver/jni/impl/OwnedReferences.java New resource-tracking helper for closing child resources.
java/driver/jni/src/main/java/org/apache/arrow/adbc/driver/jni/impl/NativeQueryResult.java Updates stream import to return a TiedArrowReader associated with the producing resource.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@lidavidm lidavidm changed the title feat(java/driver/jni): prevent closing statement before connection feat(java/driver/jni): close child resources automatically Jun 25, 2026
@lidavidm lidavidm requested a review from Copilot June 25, 2026 05:38

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.

Comment thread java/driver/jni/src/test/java/org/apache/arrow/adbc/driver/jni/impl/ImplTest.java Outdated
Comment thread java/driver/jni/src/test/java/org/apache/arrow/adbc/driver/jni/JniDriverTest.java Outdated
@lidavidm lidavidm marked this pull request as ready for review June 25, 2026 07:09

@amoeba amoeba left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good, just one question.

if (parent != null) {
parent.getChildReferences().releaseReference(this);
this.parent = null;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should this and other similar blocks be in a finally so the child always gets detached? Just curious.

@zeroshade zeroshade left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM just a few nits and I agree with @amoeba about potentially using finally blocks

Comment on lines +35 to +37
public ChildReferences() {
this.openReferences = Collections.newSetFromMap(new WeakHashMap<>());
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should we add a one-line comment just to point out that WeakHashMap isn't thread-safe so that we don't overlook that in any future changes?

Comment on lines 53 to 55
public void close() throws Exception {
AutoCloseables.close(childReferences, handle);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

JniConnection.close() and JniStatement.close() wrap failures with AdbcException.internal(...) but here we're just declaring throws exception and letting it propagate. Any reason for the difference? Should we wrap here also for consistency?

refs.close();
}

static final class Closeable implements AutoCloseable {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this shadows the java.io.Closeable import which ends up being unused I think. Should we drop the import?

@lidavidm lidavidm merged commit d3a4522 into apache:main Jun 25, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants