Backend API. Server-side code: this runs in a Codename One backend, not in the app on the device.
public final class DbPool
- Object
- DbPool
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.
Methods
public static DbPool open(String path, int size, int busyTimeoutMillis)
throws IOException | Opens size connections to path and puts the database in WAL mode. |
public synchronized Db borrow()
throws IOException | Takes a connection, blocking until one is free. |
public synchronized void release(Db db) | |
public Object withConnection(Db.Work body)
throws Exception | Borrows a connection, runs body, and returns it however body ends. |
public Object inTransaction(Db.Work body)
throws Exception | Convenience: one transaction on a pooled connection. |
public synchronized void close() |
Inherited methods
Method details
open
public static DbPool open(String path, int size, int busyTimeoutMillis)
throws IOExceptionOpens 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
borrow
public synchronized Db borrow()
throws IOExceptionwithConnection, which cannot leak one.Throws
release
public synchronized void release(Db db)withConnection
public Object withConnection(Db.Work body)
throws ExceptionThrows
inTransaction
public Object inTransaction(Db.Work body)
throws ExceptionThrows
close
public synchronized void close()