From d5b92744edaf197814b278c7f561948330300a1d Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Thu, 3 Oct 2019 14:09:43 +0200 Subject: [PATCH] increase number of simultan database-connections, wait for write if another thread writes - increase the number of simultan database-connections handled by the r2d2 pool. currently we have already at least 5 threads threads, but also the ui may call from any thread. - the busy-timeout for all connections is set to 10 seconds. this means, if a connection-A wants to write, but connection B-is already writing, connection-A waits multiple times a few ms and tries over. this is repeated until the 10 seconds are accumulated. --- src/sql.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/sql.rs b/src/sql.rs index e4daf6a50..6e14c12f2 100644 --- a/src/sql.rs +++ b/src/sql.rs @@ -1,5 +1,6 @@ use std::collections::HashSet; use std::sync::{Arc, RwLock}; +use std::time::Duration; use rusqlite::{Connection, OpenFlags, Statement, NO_PARAMS}; use thread_local_object::ThreadLocal; @@ -68,6 +69,16 @@ impl Sql { let res = match &*self.pool.read().unwrap() { Some(pool) => { let conn = pool.get()?; + + // Only one process can make changes to the database at one time. + // busy_timeout defines, that if a seconds process wants write access, + // this second process will wait some milliseconds + // and try over until it gets write access or the given timeout is elapsed. + // If the second process does not get write access within the given timeout, + // sqlite3_step() will return the error SQLITE_BUSY. + // (without a busy_timeout, sqlite3_step() would return SQLITE_BUSY _at once_) + conn.busy_timeout(Duration::from_secs(10))?; + g(&conn) } None => Err(Error::SqlNoConnection), @@ -330,7 +341,7 @@ fn open( .with_init(|c| c.execute_batch("PRAGMA secure_delete=on;")); let pool = r2d2::Pool::builder() .min_idle(Some(2)) - .max_size(4) + .max_size(10) .connection_timeout(std::time::Duration::new(60, 0)) .build(mgr)?;