Module sqlx_core::pool

source ·
Expand description

Provides the connection pool for asynchronous SQLx connections.

Opening a database connection for each and every operation to the database can quickly become expensive. Furthermore, sharing a database connection between threads and functions can be difficult to express in Rust.

A connection pool is a standard technique that can manage opening and re-using connections. Normally it also enforces a maximum number of connections as these are an expensive resource on the database server.

SQLx provides a canonical connection pool implementation intended to satisfy the majority of use cases.

See Pool for details.

Type aliases are provided for each database to make it easier to sprinkle Pool through your codebase:

Opening a connection pool

A new connection pool with a default configuration can be created by supplying Pool with the database driver and a connection string.

use sqlx::Pool;
use sqlx::postgres::Postgres;

let pool = Pool::<Postgres>::connect("postgres://").await?;

For convenience, database-specific type aliases are provided:

use sqlx::mssql::MssqlPool;

let pool = MssqlPool::connect("mssql://").await?;

Using a connection pool

A connection pool implements Executor and can be used directly when executing a query. Notice that only an immutable reference (&Pool) is needed.

sqlx::query("DELETE FROM articles").execute(&pool).await?;

A connection or transaction may also be manually acquired with Pool::acquire or Pool::begin.

Structs