Skip to main content

ConnectionFactory

Trait ConnectionFactory 

Source
pub trait ConnectionFactory<T>: Send + Sync {
    // Required method
    fn create(&self) -> Result<T, FactoryError>;

    // Provided methods
    fn validate(&self, _conn: &T) -> bool { ... }
    fn reset(&self, _conn: &T) -> Result<(), FactoryError> { ... }
    fn close(&self, _conn: &T) -> Result<(), FactoryError> { ... }
    fn is_fatal_error(&self, _err: &(dyn Error + 'static)) -> bool { ... }
}
Expand description

Implement this to pool anything (DB driver, HTTP client, SSH session, socket…) — or build one from closures via FnFactory.

create must return a distinct object each call; the pool tracks borrow state by value identity and rejects a factory that returns an object it already manages.

Required Methods§

Source

fn create(&self) -> Result<T, FactoryError>

Opens a new connection. Should enforce its own connect timeout.

Provided Methods§

Source

fn validate(&self, _conn: &T) -> bool

A cheap liveness check (e.g. SELECT 1). false = dead. Defaults to true.

Source

fn reset(&self, _conn: &T) -> Result<(), FactoryError>

Called on release to scrub session state (e.g. rollback). Defaults to a no-op.

Source

fn close(&self, _conn: &T) -> Result<(), FactoryError>

Disposes of a connection. Defaults to a no-op — most Rust connections close on Drop; override only when explicit teardown is needed.

Source

fn is_fatal_error(&self, _err: &(dyn Error + 'static)) -> bool

Whether err (raised by user code using the connection) proves the connection is broken, so it is discarded without further checks. Defaults to false.

Trait Implementations§

Source§

impl<T> ConnectionFactory<T> for Box<dyn ConnectionFactory<T>>

Lets a boxed factory be used anywhere a ConnectionFactory<T> is expected (e.g. a PoolRegistry whose per-key factories have different concrete types).

Source§

fn create(&self) -> Result<T, FactoryError>

Opens a new connection. Should enforce its own connect timeout.
Source§

fn validate(&self, conn: &T) -> bool

A cheap liveness check (e.g. SELECT 1). false = dead. Defaults to true.
Source§

fn reset(&self, conn: &T) -> Result<(), FactoryError>

Called on release to scrub session state (e.g. rollback). Defaults to a no-op.
Source§

fn close(&self, conn: &T) -> Result<(), FactoryError>

Disposes of a connection. Defaults to a no-op — most Rust connections close on Drop; override only when explicit teardown is needed.
Source§

fn is_fatal_error(&self, err: &(dyn Error + 'static)) -> bool

Whether err (raised by user code using the connection) proves the connection is broken, so it is discarded without further checks. Defaults to false.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<T> ConnectionFactory<T> for Box<dyn ConnectionFactory<T>>

Lets a boxed factory be used anywhere a ConnectionFactory<T> is expected (e.g. a PoolRegistry whose per-key factories have different concrete types).

Source§

fn create(&self) -> Result<T, FactoryError>

Source§

fn validate(&self, conn: &T) -> bool

Source§

fn reset(&self, conn: &T) -> Result<(), FactoryError>

Source§

fn close(&self, conn: &T) -> Result<(), FactoryError>

Source§

fn is_fatal_error(&self, err: &(dyn Error + 'static)) -> bool

Implementors§

Source§

impl<T> ConnectionFactory<T> for FnFactory<T>
where T: Send + Sync,