Class 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.
-
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 preferwithConnection(Db.Work), which cannot leak one.- Throws:
IOException
-
release
-
withConnection
-
inTransaction
-
close
public void close()
-