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, when SQLite is compiled SQLITE_THREADSAFE=1 and a single connection is already safe to share: serialized mode makes concurrent use SAFE by serializing it, which means 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.
      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()