From 75806fdcc78643648eec085a5fb649377ff2faa0 Mon Sep 17 00:00:00 2001 From: yzc1114 Date: Thu, 12 Jun 2025 21:26:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BAsqlite=E6=B7=BB=E5=8A=A0WAL=E6=9C=BA?= =?UTF-8?q?=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/db/db.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/db/db.rs b/src/db/db.rs index 887ebe9..b704f46 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(()) + } +} -- Gitee