Skip to main content
Version: 0.11.x

Connection Pool

To obtain a database connection, use the Database interface:

let db: DatabaseConnection = Database::connect("protocol://username:password@host/database").await?;

protocol can be mysql:, postgres: or sqlite:.

host is usually localhost, a domain name or an IP address.

Under the hood, a sqlx::Pool is created and owned by DatabaseConnection.

Each time you call execute or query_one/all on it, a connection will be acquired and released from the pool.

Multiple queries will execute in parallel as you await on them.

Connect Optionsโ€‹

To configure the connection, use the ConnectOptions interface:

let mut opt = ConnectOptions::new("protocol://username:password@host/database".to_owned());
opt.max_connections(100)
.min_connections(5)
.connect_timeout(Duration::from_secs(8))
.acquire_timeout(Duration::from_secs(8))
.idle_timeout(Duration::from_secs(8))
.max_lifetime(Duration::from_secs(8))
.sqlx_logging(true)
.sqlx_logging_level(log::LevelFilter::Info)
.set_schema_search_path("my_schema".into()); // Setting default PostgreSQL schema

let db = Database::connect(opt).await?;

Closing Connectionโ€‹

The connection will be automatically closed on drop. To close the connection explicitly, call the close method.

let db = Database::connect(url).await?;

// Closing connection here
db.close().await?;