How to use connection pooling

Use the r2d2 crate with Diesel to create a Pool that manages reusable database connections for better performance.

Use the r2d2 crate to manage a pool of database connections for Diesel. Add diesel and r2d2 to your Cargo.toml, then instantiate a Pool with your connection manager and use it to run queries.

use diesel::r2d2::{self, ConnectionManager, PooledConnection};
use diesel::SqliteConnection;

fn main() {
    let manager = ConnectionManager::<SqliteConnection>::new("file:mydb.sqlite");
    let pool = r2d2::Pool::builder().build(manager).unwrap();
    let mut conn = pool.get().unwrap();
    // Run your query here
}