How to Use Connection Pools in Rust (deadpool, bb8)

Initialize a deadpool-diesel Pool with a Manager and Runtime, then fetch connections via pool.get().await.

Use deadpool-diesel to create a Pool with a Manager configured for your database and runtime, then retrieve connections via pool.get().await.

use deadpool_diesel::postgres::{Manager, Pool};

let manager = Manager::new(db_url, deadpool_diesel::Runtime::Tokio1);
let pool = Pool::builder(manager).build().unwrap();
let conn = pool.get().await.unwrap();
  1. Add the deadpool-diesel crate with the postgres feature to your Cargo.toml.
  2. Create a Manager using your DATABASE_URL and the Tokio1 runtime.
  3. Build the Pool from the manager using Pool::builder.
  4. Retrieve a connection from the pool by calling pool.get().await inside your async handler.