Skip to main content

ConnectionPool

Struct ConnectionPool 

Source
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

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn available_readers(&self) -> usize

Get the current number of available reader connections.

Source

pub fn max_readers(&self) -> usize

Get the total number of reader connections in the pool.

Source

pub fn config(&self) -> &PoolConfig

Return the pool configuration.

Source

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().

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.