PoolOptions

Struct PoolOptions 

Source
pub struct PoolOptions<DB: Database> {
    pub test_before_acquire: bool,
    pub after_connect: Option<Box<dyn Fn(&mut DB::Connection) -> Result<(), Error> + Send + Sync + 'static>>,
    pub before_acquire: Option<Box<dyn Fn(&mut DB::Connection) -> Result<bool, Error> + Send + Sync + 'static>>,
    pub after_release: Option<Box<dyn Fn(&mut DB::Connection) -> bool + Send + Sync + 'static>>,
    pub max_connections: u32,
    pub connect_timeout: Duration,
    pub min_connections: u32,
    pub max_lifetime: Option<Duration>,
    pub idle_timeout: Option<Duration>,
}

Fields§

§test_before_acquire: bool§after_connect: Option<Box<dyn Fn(&mut DB::Connection) -> Result<(), Error> + Send + Sync + 'static>>§before_acquire: Option<Box<dyn Fn(&mut DB::Connection) -> Result<bool, Error> + Send + Sync + 'static>>§after_release: Option<Box<dyn Fn(&mut DB::Connection) -> bool + Send + Sync + 'static>>§max_connections: u32§connect_timeout: Duration§min_connections: u32§max_lifetime: Option<Duration>§idle_timeout: Option<Duration>

Implementations§

Source§

impl<DB: Database> PoolOptions<DB>

Source

pub fn new() -> Self

Source

pub fn max_connections(self, max: u32) -> Self

Set the maximum number of connections that this pool should maintain.

Source

pub fn connect_timeout(self, timeout: Duration) -> Self

Set the amount of time to attempt connecting to the database.

If this timeout elapses, Pool::acquire will return an error.

Source

pub fn min_connections(self, min: u32) -> Self

Set the minimum number of connections to maintain at all times.

When the pool is built, this many connections will be automatically spun up.

If any connection is reaped by max_lifetime or idle_timeout and it brings the connection count below this amount, a new connection will be opened to replace it.

Source

pub fn max_lifetime(self, lifetime: impl Into<Option<Duration>>) -> Self

Set the maximum lifetime of individual connections.

Any connection with a lifetime greater than this will be closed.

When set to None, all connections live until either reaped by idle_timeout or explicitly disconnected.

Infinite connections are not recommended due to the unfortunate reality of memory/resource leaks on the database-side. It is better to retire connections periodically (even if only once daily) to allow the database the opportunity to clean up data structures (parse trees, query metadata caches, thread-local storage, etc.) that are associated with a session.

Source

pub fn idle_timeout(self, timeout: impl Into<Option<Duration>>) -> Self

Set a maximum idle duration for individual connections.

Any connection with an idle duration longer than this will be closed.

For usage-based database server billing, this can be a cost saver.

Source

pub fn test_before_acquire(self, test: bool) -> Self

If true, the health of a connection will be verified by a call to Connection::ping before returning the connection.

Defaults to true.

Source

pub fn after_connect<F>(self, callback: F) -> Self
where for<'c> F: Fn(&'c mut DB::Connection) -> Result<(), Error> + 'static + Send + Sync,

Perform an action after connecting to the database.

§Example
use cdbc::executor::Executor;
use cdbc_pg::PgPoolOptions;
// PostgreSQL
let pool = PgPoolOptions::new()
    .after_connect(|conn| {
       conn.execute("SET application_name = 'your_app';")?;
       conn.execute("SET search_path = 'my_schema';")?;

       Ok(())
    })
    .connect("postgres:// …")?;
Source

pub fn before_acquire<F>(self, callback: F) -> Self
where for<'c> F: Fn(&'c mut DB::Connection) -> Result<bool, Error> + 'static + Send + Sync,

Source

pub fn after_release<F>(self, callback: F) -> Self
where F: Fn(&mut DB::Connection) -> bool + 'static + Send + Sync,

Source

pub fn connect(self, uri: &str) -> Result<Pool<DB>, Error>

Creates a new pool from this configuration and immediately establishes one connection.

Source

pub fn connect_with( self, options: <DB::Connection as Connection>::Options, ) -> Result<Pool<DB>, Error>

Creates a new pool from this configuration and immediately establishes one connection.

Source

pub fn connect_lazy(self, uri: &str) -> Result<Pool<DB>, Error>

Creates a new pool from this configuration and will establish a connections as the pool starts to be used.

Source

pub fn connect_lazy_with( self, options: <DB::Connection as Connection>::Options, ) -> Pool<DB>

Creates a new pool from this configuration and will establish a connections as the pool starts to be used.

Trait Implementations§

Source§

impl<DB: Database> Debug for PoolOptions<DB>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<DB: Database> Default for PoolOptions<DB>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<DB> Freeze for PoolOptions<DB>

§

impl<DB> !RefUnwindSafe for PoolOptions<DB>

§

impl<DB> Send for PoolOptions<DB>

§

impl<DB> Sync for PoolOptions<DB>

§

impl<DB> Unpin for PoolOptions<DB>

§

impl<DB> !UnwindSafe for PoolOptions<DB>

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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.