@@ -359,16 +359,34 @@ static String getCanonicalPath(File directory) {
359359 }
360360
361361 static void verifyNotAlreadyOpen (String canonicalPath ) {
362+ // Call isFileOpen before, but without checking the result, to try to close any unreferenced instances where
363+ // it was forgotten to close them.
364+ // Only obtain the lock on openFiles afterward as the checker thread created by isFileOpen needs to obtain it to
365+ // do anything.
366+ isFileOpen (canonicalPath );
362367 synchronized (openFiles ) {
363- isFileOpen (canonicalPath ); // for retries
364368 if (!openFiles .add (canonicalPath )) {
365- throw new DbException ("Another BoxStore is still open for this directory: " + canonicalPath +
366- ". Hint: for most apps it's recommended to keep a BoxStore for the app's life time ." );
369+ throw new DbException ("Another BoxStore is still open for this directory ( " + canonicalPath +
370+ "). Make sure the existing instance is explicitly closed before creating a new one ." );
367371 }
368372 }
369373 }
370374
371- /** Also retries up to 500ms to improve GC race condition situation. */
375+ /**
376+ * Returns if the given path is in {@link #openFiles}.
377+ * <p>
378+ * If it is, (creates and) briefly waits on an existing "checker" thread before checking again and returning the
379+ * result.
380+ * <p>
381+ * The "checker" thread locks {@link #openFiles} while it triggers garbage collection and finalization in this Java
382+ * Virtual Machine to try to close any unreferenced BoxStore instances. These might exist if it was forgotten to
383+ * close them explicitly.
384+ * <p>
385+ * Note that the finalization mechanism relied upon here is scheduled for removal in future versions of Java and may
386+ * already be disabled depending on JVM configuration.
387+ *
388+ * @see #finalize()
389+ */
372390 static boolean isFileOpen (final String canonicalPath ) {
373391 synchronized (openFiles ) {
374392 if (!openFiles .contains (canonicalPath )) return false ;
@@ -522,9 +540,13 @@ public long getDbSizeOnDisk() {
522540 }
523541
524542 /**
525- * Closes this if this is finalized.
543+ * Calls {@link #close()}.
544+ * <p>
545+ * It is strongly recommended to instead explicitly close a Store and not rely on finalization. For example, on
546+ * Android finalization has a timeout that might be exceeded if closing needs to wait on transactions to finish.
526547 * <p>
527- * Explicitly call {@link #close()} instead to avoid expensive finalization.
548+ * Also finalization is scheduled for removal in future versions of Java and may already be disabled depending on
549+ * JVM configuration (see documentation on super method).
528550 */
529551 @ SuppressWarnings ("deprecation" ) // finalize()
530552 @ Override
0 commit comments