Skip to content

bindings/java: Review unsafe caching policy of TursoDBFactory #2071

@LeMikaelF

Description

@LeMikaelF

TursoDBFactory currently caches the returned TursoDB database instances, with the url as the cache key, but this caching policy is unsafe WRT closing.

The problem with this is that TursoDB is an AutoCloseable, an interface that declares that the object should be closed after use, but it's not safe for the user to close a TursoDB created by the factory. Consider:

  1. caller A requests a TursoDB, the factory creates one and returns it
  2. caller B request a TursoDB for the same URL, the factory reuses the cached instance and returns it
  3. caller A completes its processing and closes its TursoDB
  4. caller B's TursoDB is now unusable because it was closed by caller A

I see a few solutions to this problem, ordered from most desirable to least desirable:

  1. don't cache TursoDB instances. This will also make it possible to open different databases to the same file but with different flags
  2. make TursoDBFactory::open non-static, and make the factory Closeable, so that callers can create a factory, and then close it when they're done. Closing it will close all tracked TursoDB instances. It would then be acceptable to make TursoDBFactory non thread-safe, since the cache would be local to the caller, and not ClassLoader-wide.
  3. from the factory, return a reference-counting wrapper around TursoDB that will keep track of how many times the factory was called for the same url, and only close the underlying instance when close() has been called on all instances.

Option 1 is the easiest, and Options 2 and 3 would likely require some additional locking, or a different thread-safety policy.

@seonWKim tagging you on this since I think you wrote most of the bindings.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions