zeroshade commented on code in PR #4441:
URL: https://github.com/apache/arrow-adbc/pull/4441#discussion_r3476051156


##########
java/driver/jni/src/main/java/org/apache/arrow/adbc/driver/jni/JniDatabase.java:
##########
@@ -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);
   }

Review Comment:
   `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?



##########
java/driver/jni/src/test/java/org/apache/arrow/adbc/driver/jni/impl/ImplTest.java:
##########
@@ -119,4 +121,43 @@ void offsetSlow() throws Exception {
     assertThat(buf.remaining()).isEqualTo(3);
     assertThat(JniLoader.INSTANCE.internalGetByteBuffer(buf)).isEqualTo(new 
byte[] {1, 2, 3});
   }
+
+  @Test
+  void childReferencesCloses() throws Exception {
+    ChildReferences refs = new ChildReferences();
+    var flag = new Closeable();
+    refs.addReference(flag);
+    refs.close();
+    assertThat(flag.closed).isTrue();
+  }
+
+  @Test
+  void childReferencesIsWeak() throws Exception {
+    ChildReferences refs = new ChildReferences();
+    var flag = new Closeable();
+    refs.addReference(flag);
+    var ref = new WeakReference<>(flag);
+    //noinspection UnusedAssignment
+    flag = null;
+
+    for (int i = 0; i < 50; i++) {
+      System.gc();
+      if (ref.get() == null) {
+        break;
+      }
+      Thread.sleep(100);
+    }
+
+    assertThat(ref.get()).isNull();
+    refs.close();
+  }
+
+  static final class Closeable implements AutoCloseable {

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



##########
java/driver/jni/src/main/java/org/apache/arrow/adbc/driver/jni/impl/ChildReferences.java:
##########
@@ -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<>());
+  }

Review Comment:
   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?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to