expression

Connection pools: detect immediate acquisition failure

A nonwaiting acquisition can fail because no connection is currently available. It is not proof the database is unhealthy. A size-one pool with its only connection held demonstrates saturation; production callers should choose an explicit wait or overload policy. Uses a disposable in-memory SQLite database, with no external database. Expected stdout (not executed): unavailable: true Common mistake: Treating a busy pool as a failed database or retrying without a bound.

Formal Syntax & Structure

rust syntaxrust
pool.try_acquire()

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() -> Result<(), Box<dyn std::error::Error>> {    let pool = sqlx::sqlite::SqlitePoolOptions::new().max_connections(1).connect("sqlite::memory:").await?;    let held = pool.acquire().await?;    let unavailable = pool.try_acquire().is_none();    assert!(unavailable);    println!("unavailable: {unavailable}");    drop(held);    pool.close().await;    Ok(())}
Rust Reference