Class DbPool
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 Summary
Modifier and TypeMethodDescriptionborrow()Takes a connection, blocking until one is free.voidclose()inTransaction(Db.Work body) Convenience: one transaction on a pooled connection.static DbPoolOpenssizeconnections topathand puts the database in WAL mode.voidwithConnection(Db.Work body) Borrows a connection, runs body, and returns it however body ends.
-
Method Details
-
open
Opens
sizeconnections topathand 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
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
-
withConnection
-
inTransaction
-
close
public void close()
-