A connection pool holds a bounded set of reusable database connections and hands them out on demand. Using a pool is almost always preferable to opening individual connections — it amortizes connection setup cost, enforces a hard concurrency limit, and automatically evicts unhealthy connections.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/launchbadge/sqlx/llms.txt
Use this file to discover all available pages before exploring further.
Pool is cheaply cloneable (it wraps an
Arc internally). Clone it freely to share a single pool across multiple tasks or across your application state.Type aliases
SQLx re-exportsPool<DB> with database-specific type aliases for convenience.
| Type alias | Database |
|---|---|
sqlx::PgPool | PostgreSQL |
sqlx::MySqlPool | MySQL / MariaDB |
sqlx::SqlitePool | SQLite |
sqlx::AnyPool | Any (runtime-selected driver) |
Pool::connect
min_connections connections (default 0), returning as soon as the first connection succeeds. The connection URL format is database-specific.
Pool::connect_lazy
acquire() call. Useful when you want to initialize pool state at startup without blocking on the database.
Pool::acquire
acquire_timeout (default 30 s) if all connections are busy. The connection is returned to the pool automatically when the PoolConnection guard is dropped.
Pool::begin
Transaction guard; call .commit() to commit or let it drop to roll back.
Pool::close
PoolOptions
PoolOptions<DB> is the builder used to configure a pool before connecting. Every setter takes self by value and returns Self, so calls can be chained.
Configuration reference
| Option | Default | Description |
|---|---|---|
max_connections | 10 | Maximum number of open connections in the pool. |
min_connections | 0 | Connections to maintain at all times; new ones are opened in the background if the count falls below this. |
acquire_timeout | 30 s | Maximum time to wait for a free connection before returning Error::PoolTimedOut. |
idle_timeout | 10 min | How long a connection may sit idle before it is closed and removed. Set to None to disable. |
max_lifetime | 30 min | Maximum total age of any connection. Connections older than this are closed on release. Set to None to disable. |
test_before_acquire | true | Whether to send a health-check ping before returning a connection from the idle queue. |
The default of
max_connections = 10 is intentionally conservative. Most production workloads should raise this value; check your database’s per-user connection limit first.Lifecycle hooks
Hooks let you run async code at key points in a connection’s lifetime. Each hook receives a mutable reference to the connection and a [PoolConnectionMetadata] struct containing age and idle_for durations.
after_connect
Called once, immediately after a new connection is opened. Use it to set session-level settings (e.g., SET search_path, SET time zone).
before_acquire
Called each time a connection is about to be handed out. Return Ok(true) to allow the connection, Ok(false) to close it and acquire a fresh one, or Err(_) to propagate an error.
after_release
Called each time a connection is returned to the pool. Return Ok(true) to keep it, Ok(false) to discard it.
All hooks must return
BoxFuture<'_, Result<_, Error>>. Wrap your async block with Box::pin(async move { ... }).