PoolConnection

Struct PoolConnection 

Source
pub struct PoolConnection<'a> { /* private fields */ }
Expand description

a RAII type for connection. it manages the lifetime of connection and it’s Statement cache. a set of public is exposed to interact with them.

§Caching

PoolConnection contains cache set of Statement to speed up regular used sql queries. when calling Execute::execute on a StatementNamed with &PoolConnection the pool connection does nothing special and function the same as a regular Client. In order to utilize the cache caller must execute the named statement with &mut PoolConnection. With a mutable reference of pool connection it will do local cache look up for statement and hand out one in the type of Statement if any found. If no copy is found in the cache pool connection will prepare a new statement and insert it into the cache.

§Examples

let mut conn = pool.get().await?;
// prepare a statement without caching
Statement::named("SELECT 1", &[]).execute(&conn).await?;
// prepare a statement with caching from conn.
Statement::named("SELECT 1", &[]).execute(&mut conn).await?;
  • When to use caching or not:
  • query statement repeatedly called intensely can benefit from cache.
  • query statement with low latency requirement can benefit from upfront cache.
  • rare query statement can benefit from no caching by reduce resource usage from the server side. For low latency of rare query consider use StatementNamed::bind as alternative.

Implementations§

Source§

impl PoolConnection<'_>

Source

pub fn transaction( &mut self, ) -> impl Future<Output = Result<Transaction<&mut Self>, Error>> + Send

function the same as Client::transaction

Source

pub fn transaction_owned( self, ) -> impl Future<Output = Result<Transaction<Self>, Error>> + Send

owned version of PoolConnection::transaction

Source

pub fn copy_in( &mut self, stmt: &Statement, ) -> impl Future<Output = Result<CopyIn<'_, Self>, Error>> + Send

function the same as Client::copy_in

Source

pub async fn copy_out(&self, stmt: &Statement) -> Result<CopyOut, Error>

function the same as Client::copy_out

Source

pub fn consume(self) -> Self

a shortcut to move and take ownership of self. an important behavior of PoolConnection is it supports pipelining. eagerly drop it after usage can contribute to more queries being pipelined. especially before any await point.

§Examples
use xitca_postgres::{pool::Pool, Error, Execute};

async fn example(pool: &Pool) -> Result<(), Error> {
    // get a connection from pool and start a query.
    let mut conn = pool.get().await?;

    "SELECT *".execute(&conn).await?;
     
    // connection is kept across await point. making it unusable to other concurrent
    // callers to example function. and no pipelining will happen until it's released.
    conn = conn;

    // start another query but this time consume ownership and when res is returned
    // connection is dropped and went back to pool.
    let res = "SELECT *".execute(&conn.consume());

    // connection can't be used anymore in this scope but other concurrent callers
    // to example function is able to use it and if they follow the same calling
    // convention pipelining could happen and reduce syscall overhead.

    // let res = "SELECT *".execute(&conn);

    // without connection the response can still be collected asynchronously
    res.await?;

    // therefore a good calling convention for independent queries could be:
    let conn = pool.get().await?;
    let res1 = "SELECT *".execute(&conn);
    let res2 = "SELECT *".execute(&conn);
    let res3 = "SELECT *".execute(&conn.consume());

    // all three queries can be pipelined into a single write syscall. and possibly
    // even more can be pipelined after conn.consume() is called if there are concurrent
    // callers use the same connection.
     
    res1.await?;
    res2.await?;
    res3.await?;

    // it should be noted that pipelining is an optional crate feature for some potential
    // performance gain.
    // it's totally fine to ignore and use the apis normally with zero thought put into it.

    Ok(())
}
Source

pub fn cancel_token(&self) -> Session

function the same as Client::cancel_token

Trait Implementations§

Source§

impl ClientBorrowMut for PoolConnection<'_>

Source§

fn _borrow_mut(&mut self) -> &mut Client

Source§

impl Copy for PoolConnection<'_>

Source§

fn send_one_way<F>(&self, func: F) -> Result<(), Error>
where F: FnOnce(&mut BytesMut) -> Result<(), Error>,

Source§

impl Drop for PoolConnection<'_>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<'c, 's> Execute<&'c mut PoolConnection<'_>> for StatementNamed<'s>
where 's: 'c,

Source§

type ExecuteOutput = StatementCacheFuture<'c>

outcome of execute. used for single time database response: number of rows affected by execution for example.
Source§

type QueryOutput = <StatementNamed<'s> as Execute<&'c mut PoolConnection<'_>>>::ExecuteOutput

outcome of query. used for repeated database response: database rows for example Read more
Source§

fn execute(self, cli: &'c mut PoolConnection<'_>) -> Self::ExecuteOutput

define how a statement is executed with single time response
Source§

fn query(self, cli: &'c mut PoolConnection<'_>) -> Self::QueryOutput

define how a statement is queried with repeated response
Source§

impl<'c, 's, P> Execute<&'c mut PoolConnection<'_>> for StatementQuery<'s, P>
where P: AsParams + Send + 'c, 's: 'c,

Available on non-crate feature nightly only.
Source§

type ExecuteOutput = Pin<Box<dyn Future<Output = Result<RowAffected, Error>> + Send + 'c>>

outcome of execute. used for single time database response: number of rows affected by execution for example.
Source§

type QueryOutput = Pin<Box<dyn Future<Output = Result<GenericRowStream<Arc<[Column]>, Typed>, Error>> + Send + 'c>>

outcome of query. used for repeated database response: database rows for example Read more
Source§

fn execute(self, conn: &'c mut PoolConnection<'_>) -> Self::ExecuteOutput

define how a statement is executed with single time response
Source§

fn query(self, conn: &'c mut PoolConnection<'_>) -> Self::QueryOutput

define how a statement is queried with repeated response
Source§

impl Prepare for PoolConnection<'_>

Source§

async fn _get_type(&self, oid: Oid) -> Result<Type, Error>

Source§

fn _get_type_blocking(&self, oid: Oid) -> Result<Type, Error>

Source§

impl Query for PoolConnection<'_>

Source§

fn _send_encode_query<S>(&self, stmt: S) -> Result<(S::Output, Response), Error>
where S: Encode,

encode statement and params and send it to client driver
Source§

fn _query<S>( &self, stmt: S, ) -> Result<<S::Output as IntoResponse>::Response, Error>
where S: Encode,

query with statement and dynamic typed params. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for PoolConnection<'a>

§

impl<'a> RefUnwindSafe for PoolConnection<'a>

§

impl<'a> Send for PoolConnection<'a>

§

impl<'a> Sync for PoolConnection<'a>

§

impl<'a> Unpin for PoolConnection<'a>

§

impl<'a> UnwindSafe for PoolConnection<'a>

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

Source§

type Output = T

Should always be Self
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V