fix(db): serialize SQLite WAL establishment before pooling
Switching a brand-new DB file to WAL is a one-time file-level change; letting multiple pooled connections perform it concurrently during warm-up races the switch and can surface a spurious lock (observed as intermittent 500s under the integration harness). Open ONE connection to establish WAL before the pool opens, so every pooled connection thereafter only re-asserts an already-WAL file. Keeps per-connection foreign_keys + busy_timeout.
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use sqlx::{
|
use sqlx::{
|
||||||
sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions},
|
sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions},
|
||||||
SqlitePool,
|
ConnectOptions, Connection, SqlitePool,
|
||||||
};
|
};
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
@@ -11,16 +11,26 @@ pub type Pool = SqlitePool;
|
|||||||
|
|
||||||
pub async fn init_pool(database_url: &str, max_connections: u32) -> Result<Pool> {
|
pub async fn init_pool(database_url: &str, max_connections: u32) -> Result<Pool> {
|
||||||
info!("Connecting to database: {}", database_url);
|
info!("Connecting to database: {}", database_url);
|
||||||
// Per-connection options so EVERY pooled connection gets them (a one-off
|
// Per-connection options so EVERY pooled connection gets them: WAL for
|
||||||
// `PRAGMA` on the pool only configures whichever connection served it):
|
// reader/writer concurrency, foreign keys on, and a busy_timeout so a
|
||||||
// WAL for reader/writer concurrency, foreign keys on, and a busy_timeout so
|
// transient SQLITE_BUSY under concurrent access waits-and-retries.
|
||||||
// a transient SQLITE_BUSY under concurrent access waits-and-retries instead
|
|
||||||
// of failing the transaction.
|
|
||||||
let opts = SqliteConnectOptions::from_str(database_url)?
|
let opts = SqliteConnectOptions::from_str(database_url)?
|
||||||
.create_if_missing(true)
|
.create_if_missing(true)
|
||||||
.journal_mode(SqliteJournalMode::Wal)
|
.journal_mode(SqliteJournalMode::Wal)
|
||||||
.foreign_keys(true)
|
.foreign_keys(true)
|
||||||
.busy_timeout(Duration::from_secs(5));
|
.busy_timeout(Duration::from_secs(5));
|
||||||
|
// Establish WAL on the file via ONE connection BEFORE the pool opens.
|
||||||
|
// Switching a fresh DB to WAL is a one-time file-level change; letting
|
||||||
|
// several pooled connections do it concurrently at warm-up races that
|
||||||
|
// switch and can surface a spurious lock. Serialize it here so every
|
||||||
|
// pooled connection thereafter only re-asserts an already-WAL file.
|
||||||
|
{
|
||||||
|
let mut conn = opts.clone().connect().await?;
|
||||||
|
sqlx::query("PRAGMA journal_mode=WAL")
|
||||||
|
.execute(&mut conn)
|
||||||
|
.await?;
|
||||||
|
conn.close().await?;
|
||||||
|
}
|
||||||
let pool = SqlitePoolOptions::new()
|
let pool = SqlitePoolOptions::new()
|
||||||
.max_connections(max_connections)
|
.max_connections(max_connections)
|
||||||
.connect_with(opts)
|
.connect_with(opts)
|
||||||
|
|||||||
Reference in New Issue
Block a user