Trait sqlx_core::connection::Connection
source · pub trait Connection: Send {
type Database: Database;
type Options: ConnectOptions<Connection = Self>;
// Required methods
fn close(self) -> BoxFuture<'static, Result<(), Error>>;
fn ping(&mut self) -> BoxFuture<'_, Result<(), Error>>;
fn begin(
&mut self,
) -> BoxFuture<'_, Result<Transaction<'_, Self::Database>, Error>>
where Self: Sized;
fn shrink_buffers(&mut self);
// Provided methods
fn transaction<'a, F, R, E>(
&'a mut self,
callback: F,
) -> BoxFuture<'a, Result<R, E>>
where for<'c> F: FnOnce(&'c mut Transaction<'_, Self::Database>) -> BoxFuture<'c, Result<R, E>> + 'a + Send + Sync,
Self: Sized,
R: Send,
E: From<Error> + Send { ... }
fn cached_statements_size(&self) -> usize
where Self::Database: HasStatementCache { ... }
fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>>
where Self::Database: HasStatementCache { ... }
fn connect(url: &str) -> BoxFuture<'static, Result<Self, Error>>
where Self: Sized { ... }
fn connect_with(
options: &Self::Options,
) -> BoxFuture<'_, Result<Self, Error>>
where Self: Sized { ... }
}
Expand description
Represents a single database connection.
Required Associated Types§
Required Methods§
sourcefn close(self) -> BoxFuture<'static, Result<(), Error>>
fn close(self) -> BoxFuture<'static, Result<(), Error>>
Explicitly close this database connection.
This notifies the database server that the connection is closing so that it can free up any server-side resources in use.
While connections can simply be dropped to clean up local resources,
the Drop
handler itself cannot notify the server that the connection is being closed
because that may require I/O to send a termination message. That can result in a delay
before the server learns that the connection is gone, usually from a TCP keepalive timeout.
Creating and dropping many connections in short order without calling .close()
may
lead to errors from the database server because those senescent connections will still
count against any connection limit or quota that is configured.
Therefore it is recommended to call .close()
on a connection when you are done using it
and to .await
the result to ensure the termination message is sent.
sourcefn ping(&mut self) -> BoxFuture<'_, Result<(), Error>>
fn ping(&mut self) -> BoxFuture<'_, Result<(), Error>>
Checks if a connection to the database is still valid.
sourcefn begin(
&mut self,
) -> BoxFuture<'_, Result<Transaction<'_, Self::Database>, Error>>where
Self: Sized,
fn begin(
&mut self,
) -> BoxFuture<'_, Result<Transaction<'_, Self::Database>, Error>>where
Self: Sized,
Begin a new transaction or establish a savepoint within the active transaction.
Returns a Transaction
for controlling and tracking the new transaction.
sourcefn shrink_buffers(&mut self)
fn shrink_buffers(&mut self)
Restore any buffers in the connection to their default capacity, if possible.
Sending a large query or receiving a resultset with many columns can cause the connection to allocate additional buffer space to fit the data which is retained afterwards in case it’s needed again. This can give the outward appearance of a memory leak, but is in fact the intended behavior.
Calling this method tells the connection to release that excess memory if it can, though be aware that calling this too often can cause unnecessary thrashing or fragmentation in the global allocator. If there’s still data in the connection buffers (unlikely if the last query was run to completion) then it may need to be moved to allow the buffers to shrink.
Provided Methods§
sourcefn transaction<'a, F, R, E>(
&'a mut self,
callback: F,
) -> BoxFuture<'a, Result<R, E>>
fn transaction<'a, F, R, E>( &'a mut self, callback: F, ) -> BoxFuture<'a, Result<R, E>>
Execute the function inside a transaction.
If the function returns an error, the transaction will be rolled back. If it does not return an error, the transaction will be committed.
§Example
use sqlx::postgres::{PgConnection, PgRow};
use sqlx::Connection;
conn.transaction(|txn| Box::pin(async move {
sqlx::query("select * from ..").fetch_all(&mut **txn).await
})).await
sourcefn cached_statements_size(&self) -> usizewhere
Self::Database: HasStatementCache,
fn cached_statements_size(&self) -> usizewhere
Self::Database: HasStatementCache,
The number of statements currently cached in the connection.
sourcefn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>>where
Self::Database: HasStatementCache,
fn clear_cached_statements(&mut self) -> BoxFuture<'_, Result<(), Error>>where
Self::Database: HasStatementCache,
Removes all statements from the cache, closing them on the server if needed.