pub struct ConnectionPool { /* private fields */ }Expand description
A read-write connection pool for SQLite.
Architecture:
- 1 writer connection protected by a Mutex (exclusive access)
- N reader connections in a lock-free queue (concurrent access)
- All connections share the same database file in WAL mode
For in-memory databases, or when WAL mode is disabled/unavailable, the pool degrades to single-connection mode and routes all operations through the writer connection.
Implementations§
Source§impl ConnectionPool
impl ConnectionPool
Sourcepub fn new(config: PoolConfig) -> Result<Self, SqliteError>
pub fn new(config: PoolConfig) -> Result<Self, SqliteError>
Create a new connection pool.
Opens 1 writer + N reader connections to the same database when pooling is enabled. All connections are configured consistently (busy timeout, foreign keys, cache, mmap, temp store). For in-memory databases, or when WAL is disabled or unavailable, the pool falls back to single-connection mode.
Sourcepub fn reader(&self) -> Result<ReaderGuard<'_>, SqliteError>
pub fn reader(&self) -> Result<ReaderGuard<'_>, SqliteError>
Check out a reader connection.
Tries to pop from the lock-free queue. If empty, spins briefly then
waits with exponential backoff up to checkout_timeout.
§Deadlock Warning
In degraded mode (WAL unavailable, max_readers == 0), this method locks
the writer mutex. If the calling thread already holds a WriterGuard,
this will deadlock (parking_lot Mutex is not reentrant). Never call
reader() while holding a WriterGuard on the same pool.
Sourcepub fn writer(&self) -> Result<WriterGuard<'_>, SqliteError>
pub fn writer(&self) -> Result<WriterGuard<'_>, SqliteError>
Check out the writer connection.
Waits up to checkout_timeout for the writer Mutex and returns
Err(SqliteError::InvalidData) if the timeout is exceeded.
Sourcepub fn try_writer(&self) -> Result<WriterGuard<'_>, SqliteError>
pub fn try_writer(&self) -> Result<WriterGuard<'_>, SqliteError>
Non-panicking writer checkout.
Returns Err on timeout instead of panicking. Use this in request
handlers where a 500 is preferable to crashing the process.
Sourcepub fn available_readers(&self) -> usize
pub fn available_readers(&self) -> usize
Get the current number of available reader connections.
Sourcepub fn max_readers(&self) -> usize
pub fn max_readers(&self) -> usize
Get the total number of reader connections in the pool.
Sourcepub fn config(&self) -> &PoolConfig
pub fn config(&self) -> &PoolConfig
Return the pool configuration.
Sourcepub fn legacy_conn(&self) -> Arc<Mutex<Connection>>
pub fn legacy_conn(&self) -> Arc<Mutex<Connection>>
Compatibility method: returns the writer connection wrapped in Arc
WARNING: This exists only for backward compatibility with code that
calls store.conn(). New code should use reader() and writer().