Provides a bounded set of shared sqlite::connection instances guarded by a
mutex and condition variable. Callers acquire a scoped lease; when that
lease is destroyed, the connection returns to the pool.
Use cases
- Ensures you never exceed a fixed number of open database handles—great for shared hosting quotas or when you want predictable WAL checkpoint behavior.
- Separates connection creation logic through
connection_factory, so your pool knows how to rebuild connections after transient failures. - Makes it obvious when work blocks;
acquire()waits on a condition variable, so thread dumps show exactly who is starved.
Example usage
sqlite::connection_pool pool{
4,
sqlite::connection_pool::make_factory("/srv/app.db",
sqlite::open_mode::open_existing)
};
auto worker = [&]() {
auto conn = pool.acquire(); // blocks if all 4 are busy
sqlite::query q{*conn, "SELECT count(*) FROM jobs WHERE status = ?" };
q % "pending";
for (auto const & row : q.each()) {
std::cout << row.get<int>(0) << '\n';
}
};
Prefer non-blocking calls? wrap acquire() in your own optional-and-timeout
helper—the pool intentionally keeps the core API blocking and simple.
connection_pool::lease
| Member | Description |
|---|---|
lease() | Creates an empty handle. |
lease(connection_pool*, std::shared_ptr<connection>) | Internal constructor used by the pool. |
lease(lease&&) noexcept, lease& operator=(lease&&) noexcept | Transfers ownership of the leased connection. |
lease(lease const&)=delete, lease& operator=(lease const&)=delete | Copying would duplicate ownership, so it is disabled. |
~lease() | Returns the connection to the originating pool. |
connection& operator*() const | Dereferences to the underlying connection. |
connection* operator->() const | Arrow operator for convenience. |
std::shared_ptr<connection> shared() const | Returns the owning shared pointer when needed. |
Pool API
| Member | Description |
|---|---|
using connection_factory = std::function<std::shared_ptr<connection>()> | Type alias for user-supplied factory callables. |
connection_pool(std::size_t capacity, connection_factory factory) | Creates a pool that lazily builds up to capacity connections via factory. |
static connection_factory make_factory(std::string db, open_mode mode = open_mode::open_or_create, filesystem_adapter_ptr fs = {}) | Utility for capturing open parameters without writing lambdas. |
lease acquire() | Blocks until a connection is available, then returns a lease. |
std::size_t capacity() const | Max number of connections the pool may create. |
std::size_t idle_count() const | Number of connections currently sitting unused in the pool. |
std::size_t created_count() const | Total connections created since construction. |