-
Notifications
You must be signed in to change notification settings - Fork 200
feat(java/driver/jni): close child resources automatically #4441
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,27 +21,37 @@ | |
| import org.apache.arrow.adbc.core.AdbcDatabase; | ||
| import org.apache.arrow.adbc.core.AdbcException; | ||
| import org.apache.arrow.adbc.core.TypedKey; | ||
| import org.apache.arrow.adbc.driver.jni.impl.ChildReferences; | ||
| import org.apache.arrow.adbc.driver.jni.impl.HasChildReferences; | ||
| import org.apache.arrow.adbc.driver.jni.impl.JniLoader; | ||
| import org.apache.arrow.adbc.driver.jni.impl.NativeDatabaseHandle; | ||
| import org.apache.arrow.memory.BufferAllocator; | ||
| import org.apache.arrow.util.AutoCloseables; | ||
|
|
||
| public class JniDatabase implements AdbcDatabase { | ||
| public class JniDatabase implements AdbcDatabase, HasChildReferences { | ||
| private final BufferAllocator allocator; | ||
| private final NativeDatabaseHandle handle; | ||
| private final ChildReferences childReferences; | ||
|
|
||
| public JniDatabase(BufferAllocator allocator, NativeDatabaseHandle handle) { | ||
| this.allocator = allocator; | ||
| this.handle = handle; | ||
| this.childReferences = new ChildReferences(); | ||
| } | ||
|
|
||
| @Override | ||
| public ChildReferences getChildReferences() { | ||
| return childReferences; | ||
| } | ||
|
|
||
| @Override | ||
| public AdbcConnection connect() throws AdbcException { | ||
| return new JniConnection(allocator, JniLoader.INSTANCE.openConnection(handle)); | ||
| return new JniConnection(allocator, this, JniLoader.INSTANCE.openConnection(handle)); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| handle.close(); | ||
| public void close() throws Exception { | ||
| AutoCloseables.close(childReferences, handle); | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| @Override | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.arrow.adbc.driver.jni.impl; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.Set; | ||
| import java.util.WeakHashMap; | ||
| import org.apache.arrow.util.AutoCloseables; | ||
|
|
||
| /** | ||
| * Track child resources for the ADBC FFI. | ||
| * | ||
| * <p>You are supposed to close statements before closing the connection (etc.). This class helps | ||
| * track those references to prevent misuse at runtime. | ||
| */ | ||
| public final class ChildReferences implements AutoCloseable { | ||
| private final Set<AutoCloseable> openReferences; | ||
|
|
||
| public ChildReferences() { | ||
| this.openReferences = Collections.newSetFromMap(new WeakHashMap<>()); | ||
| } | ||
|
Comment on lines
+37
to
+41
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add a one-line comment just to point out that |
||
|
|
||
| public void close() throws Exception { | ||
| try { | ||
| var closeables = new ArrayList<>(openReferences); | ||
| AutoCloseables.close(closeables); | ||
| } finally { | ||
| openReferences.clear(); | ||
| } | ||
| } | ||
|
lidavidm marked this conversation as resolved.
|
||
|
|
||
| public void addReference(AutoCloseable any) { | ||
| openReferences.add(any); | ||
| } | ||
|
|
||
| public void releaseReference(AutoCloseable any) { | ||
| openReferences.remove(any); | ||
| } | ||
|
lidavidm marked this conversation as resolved.
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.arrow.adbc.driver.jni.impl; | ||
|
|
||
| public interface HasChildReferences { | ||
| ChildReferences getChildReferences(); | ||
| } |
There was a problem hiding this comment.
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.