Class DbPool

java.lang.Object
com.codename1.backend.DbPool

public final class DbPool extends Object

A fixed pool of connections to one SQLite database.

Why a pool at all: SQLite is compiled SQLITE_THREADSAFE=2, so a connection is used by one thread at a time (Db's monitor), and one connection gives no read concurrency at all. Several connections against a WAL database do, because WAL lets readers proceed while a writer is active.

The pool is deliberately fixed-size and blocking rather than growing on demand: an unbounded pool against a single file just moves the contention into SQLite and makes the busy-timeout the thing that fails.

  • Method Details

    • open

      public static DbPool open(String path, int size, int busyTimeoutMillis) throws IOException

      Opens size connections to path and puts the database in WAL mode.

      A ":memory:" database cannot be pooled - each connection would get its OWN private database - so that is rejected rather than silently giving every caller a different empty database.

      Throws:
      IOException
    • borrow

      public Db borrow() throws IOException

      Takes a connection, blocking until one is free. Always release it in a finally, or prefer withConnection(Db.Work), which cannot leak one.

      NOT synchronized, and a virtual thread does not wait() for a connection: monitor ownership and Object.wait belong to the HOST pthread, so a virtual thread waiting inside this monitor held its host -- and every request scheduled there -- until another borrower released, which on a busy pool is the borrower that cannot run because its host is the one held. It naps outside the monitor instead, as Database's gate and Cache do; a platform thread still waits on the monitor.

      Throws:
      IOException
    • release

      public void release(Db db)
    • withConnection

      public Object withConnection(Db.Work body) throws Exception
      Borrows a connection, runs body, and returns it however body ends.
      Throws:
      Exception
    • inTransaction

      public Object inTransaction(Db.Work body) throws Exception
      Convenience: one transaction on a pooled connection.
      Throws:
      Exception
    • close

      public void close()