diff --git a/src/db/db.rs b/src/db/db.rs index 887ebe92e1218bd8557195ab9f44c639fb1399a2..b704f46f3a14586295dc6c40e5835851196f0a99 100644 --- a/src/db/db.rs +++ b/src/db/db.rs @@ -336,7 +336,11 @@ impl DbPool { Ok(DbPool::Pg(pool)) } else { let manager = ConnectionManager::::new(database_url); - let pool = Pool::builder().build(manager)?; + let pool = Pool::builder() + .max_size(5) + .connection_customizer(Box::new(WalModeEnabler)) + .connection_timeout(std::time::Duration::from_secs(60)) + .build(manager)?; // 运行迁移 let mut conn = pool.get()?; conn.run_pending_migrations(MIGRATIONS).unwrap(); @@ -345,3 +349,20 @@ impl DbPool { } } } + +#[derive(Debug)] +struct WalModeEnabler; + +impl diesel::r2d2::CustomizeConnection for WalModeEnabler { + fn on_acquire(&self, conn: &mut SqliteConnection) -> Result<(), diesel::r2d2::Error> { + // 开启 WAL 模式 + diesel::sql_query("PRAGMA journal_mode = WAL;") + .execute(conn) + .map_err(diesel::r2d2::Error::QueryError)?; + // 可选:设置 busy_timeout,避免立即返回 locked 错误 + diesel::sql_query("PRAGMA busy_timeout = 1000;") + .execute(conn) + .map_err(diesel::r2d2::Error::QueryError)?; + Ok(()) + } +}