Skip to main content

Pool

Struct Pool 

Source
pub struct Pool { /* private fields */ }
Expand description

A connection pool with LIFO ordering and fail-fast semantics.

§Example

let pool = bsql_driver_postgres::Pool::connect("postgres://user:pass@localhost/db")?;
let mut conn = pool.acquire()?;
conn.simple_query("SELECT 1")?;
// conn is returned to pool on drop

Implementations§

Source§

impl Pool

Source

pub fn connect(url: &str) -> Result<Self, DriverError>

Create a pool from a connection URL with default settings (max_size = 10).

Validates the URL but does not open any connections yet (lazy initialization).

Source

pub fn builder() -> PoolBuilder

Create a pool builder for custom configuration.

Source

pub fn acquire(&self) -> Result<PoolGuard, DriverError>

Acquire a connection from the pool.

Returns immediately with the most recently used idle connection (LIFO). If no idle connections are available and the pool is below max_size, a new connection is created. If the pool is at max_size and no acquire_timeout is set, returns DriverError::Pool immediately. If acquire_timeout is set, blocks until a connection is returned or the timeout expires.

Source

pub fn is_uds(&self) -> bool

Whether this pool uses UDS connections.

Returns true when the pool URL points to a Unix domain socket. On non-Unix platforms, always returns false.

Source

pub fn begin(&self) -> Result<Transaction, DriverError>

Begin a transaction. Acquires a connection and sends BEGIN.

Source

pub fn open_count(&self) -> usize

Current number of open connections (idle + in-use).

Source

pub fn max_size(&self) -> usize

Maximum pool size.

Source

pub fn status(&self) -> PoolStatus

Pool status metrics.

Source

pub fn set_warmup_sqls<S: Into<Box<str>>>( &self, sqls: impl IntoIterator<Item = S>, )

Set the SQL statements to pre-PREPARE on new connections.

Each SQL string is PREPAREd (Parse+Describe+Sync) on new connections before they are returned from acquire(). This eliminates the first-use Parse overhead for frequently executed queries.

Warmup errors are silently ignored — a bad warmup SQL must not prevent the connection from being usable.

§Example
let pool = bsql_driver_postgres::Pool::connect("postgres://user:pass@localhost/db")?;
pool.set_warmup_sqls([
    "SELECT id, name FROM users WHERE id = $1::int4",
    "SELECT id, title FROM tickets WHERE status = ANY($1::text[])",
]);

Set SQL statements to pre-PREPARE on new connections.

Accepts any iterator of items convertible to Box<str>:

  • ["SELECT 1", "SELECT 2"] — static &str, copied into Box
  • [format!("SET search_path TO {}", name)] — String, zero-copy move
Source

pub fn close(&self)

Close the pool. No new acquires are accepted. All idle connections are sent Terminate and dropped.

Source

pub fn is_closed(&self) -> bool

Whether the pool has been closed.

Trait Implementations§

Source§

impl Clone for Pool

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl Freeze for Pool

§

impl RefUnwindSafe for Pool

§

impl Send for Pool

§

impl Sync for Pool

§

impl Unpin for Pool

§

impl UnsafeUnpin for Pool

§

impl UnwindSafe for Pool

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.