liveBook · Manning

Connection pooling

As explained in Rust Servers, Services, and Apps

A connection pool is a managed collection of reusable database connections shared among clients or execution threads.

A connection pool is a managed collection of reusable database connections shared across clients or execution threads. In an asynchronous Rust web service, sqlx creates the pool and application state makes it available to handlers. section 4.0 section 4.3.2

A typical Actix Web integration follows this request-to-database path:

  • Read DATABASE_URL and create a PgPool during application startup. section 4.3.3
  • Store the pool in AppState and inject that state into the web application. section 4.3.2 section 4.3.3
  • Access the injected pool in a handler and pass it to database-access functions. section 4.5
  • Construct and execute SQL queries through the pool rather than opening a separate connection for each operation. section 4.5

Tutor web service

A database-backed tutor service creates a sqlx PgPool in main(), places it in AppState alongside health-check data, and exposes it to course and registration handlers. The same structure is built incrementally: first connect from vanilla Rust, then configure routes and state, then implement database operations and tests. section 4.0 section 8.1

use sqlx::postgres::PgPool;
use std::sync::Mutex;
pub struct AppState {
  pub health_check_response: String,
  pub visit_count: Mutex<u32>,
  pub db: PgPool,
}

The AppState definition makes the pool’s ownership and application-wide availability explicit. section 4.3.2

How Rust Servers, Services, and Apps covers it

How deep it goes
Explains creation, state injection, handler access, and query execution, while deferring pool configuration and internal reuse mechanics.
Examples
Uses tutor course APIs, registration handlers, DATABASE_URL, PgPool, AppState, and independently buildable implementation iterations.
What it assumes
Assumes Rust web-service structure and basic Actix concepts while introducing asynchronous PostgreSQL access and pool-based state incrementally.
What you can do after
Build it from scratch by creating a sqlx pool, injecting it into Actix state, and using it in database-access functions.
How it connects
Connects database connections with sqlx, PostgreSQL, asynchronous database access, application state, dependency injection, and handler functions.

Also explained in

Compare how 9 books explain connection pooling →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust Servers, Services, and Apps. The section links point to the parts of the book it draws on.