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][crate::pool::Pool] for details.
Type aliases are provided for each database to make it easier to sprinkle Pool through
your codebase:
- [MssqlPool][crate::mssql::MssqlPool] (MSSQL)
- [MySqlPool][crate::mysql::MySqlPool] (MySQL)
- [PgPool][crate::postgres::PgPool] (PostgreSQL)
- [SqlitePool][crate::sqlite::SqlitePool] (SQLite)
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 Pool;
use Postgres;
let pool = connect.await?;
For convenience, database-specific type aliases are provided:
use MssqlPool;
let pool = connect.await?;
Using a connection pool
A connection pool implements [Executor][crate::executor::Executor] and can be used directly
when executing a query. Notice that only an immutable reference (&Pool) is needed.
query.execute.await?;
A connection or transaction may also be manually acquired with
[Pool::acquire] or
[Pool::begin].