pub struct Pool { /* private fields */ }Expand description
A pool of reusable connections.
Implementations§
Source§impl Pool
impl Pool
Sourcepub async fn connect(
url: &str,
config: Option<PoolConfig>,
) -> Result<Self, Error>
pub async fn connect( url: &str, config: Option<PoolConfig>, ) -> Result<Self, Error>
Creates a pool from a postgres:// URL.
Pool settings are read from the URL (pool_size, min_connections,
acquire_timeout, idle_timeout, max_lifetime); when config is
Some, it overrides them entirely.
§TLS
TLS is always required; plaintext connections are not possible.
sslmode | Chain verified | Hostname verified |
|---|---|---|
require (default) | no | no |
verify-ca | yes | no |
verify-full | yes | yes |
disable, prefer and allow are rejected. verify-ca and
verify-full read trusted roots from sslrootcert (a PEM file) or the
operating system trust store.
use postgresql::{Pool, PoolConfig};
// Pool of up to 16 connections, TLS required (default).
let pool = Pool::connect("postgres://user:pass@db.example.com/app?pool_size=16", None).await?;
// Verify the certificate chain and hostname; override the pool size.
let other = Pool::connect(
"postgres://user:pass@db.example.com/app?sslmode=verify-full&sslrootcert=/etc/ssl/db-ca.pem",
Some(PoolConfig { pool_size: 32, min_connections: 2, ..PoolConfig::default() }),
)
.await?;§Errors
Returns a configuration error for an invalid URL. Opening the
min_connections is best-effort: if some cannot be established the
pool starts with fewer and opens more on demand (the minimum is not
maintained afterwards).
Sourcepub async fn get(&self) -> Result<PooledConnection, Error>
pub async fn get(&self) -> Result<PooledConnection, Error>
Acquires a connection, waiting up to the configured acquire timeout.
§Errors
Returns Error::PoolClosed if the pool is closed,
Error::PoolTimedOut if no connection becomes available in time, or a
connection error if a new connection cannot be established.
Sourcepub async fn begin(&self) -> Result<Transaction, Error>
pub async fn begin(&self) -> Result<Transaction, Error>
Acquires a connection and starts a transaction on it.