From 143eb70bee92e8225028ea00b69735a28e8c088d Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Wed, 30 Oct 2024 15:31:48 +0100 Subject: [PATCH] server: Only use a single connection with SQlite Several writer connections can lock the DB and cause other inserts to fail. A single connection should be enough given the usual workloads --- server/src/infra/database_string.rs | 6 ++++++ server/src/main.rs | 7 ++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/server/src/infra/database_string.rs b/server/src/infra/database_string.rs index 12d1b8a..67b165f 100644 --- a/server/src/infra/database_string.rs +++ b/server/src/infra/database_string.rs @@ -17,6 +17,12 @@ impl From<&str> for DatabaseUrl { } } +impl DatabaseUrl { + pub fn db_type(&self) -> &str { + self.0.scheme() + } +} + impl std::fmt::Debug for DatabaseUrl { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { if self.0.password().is_some() { diff --git a/server/src/main.rs b/server/src/main.rs index 833ff05..6f42473 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -82,9 +82,14 @@ async fn ensure_group_exists(handler: &SqlBackendHandler, group_name: &str) -> R async fn setup_sql_tables(database_url: &DatabaseUrl) -> Result { let sql_pool = { + let num_connections = if database_url.db_type() == "sqlite" { + 1 + } else { + 5 + }; let mut sql_opt = sea_orm::ConnectOptions::new(database_url.to_string()); sql_opt - .max_connections(5) + .max_connections(num_connections) .sqlx_logging(true) .sqlx_logging_level(log::LevelFilter::Debug); Database::connect(sql_opt).await?