From a6339a0710d22fd1e146fadc224a64a54c66d106 Mon Sep 17 00:00:00 2001 From: king6cong Date: Fri, 2 Jun 2017 14:28:49 +0800 Subject: [PATCH 1/2] Set default busy timeout to 5 seconds similar to: https://github.com/jgallagher/rusqlite/commit/05b03ae2cec9f9f630095d5c0e89682da334f4a4 this should reduce SQLITE_BUSY situation --- diesel/Cargo.toml | 1 + diesel/src/lib.rs | 2 ++ diesel/src/sqlite/connection/raw.rs | 16 +++++++++++++--- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/diesel/Cargo.toml b/diesel/Cargo.toml index 4b606a7884a8..a2a0b1fd4e01 100644 --- a/diesel/Cargo.toml +++ b/diesel/Cargo.toml @@ -25,6 +25,7 @@ time = { version = "0.1", optional = true } url = { version = "1.4.0", optional = true } uuid = { version = ">=0.2.0, <0.6.0", optional = true, features = ["use_std"] } ipnetwork = { version = "0.12.2", optional = true } +log = "0.3" [dev-dependencies] cfg-if = "0.1.0" diff --git a/diesel/src/lib.rs b/diesel/src/lib.rs index 2d0645897036..0507ddd8b5b2 100644 --- a/diesel/src/lib.rs +++ b/diesel/src/lib.rs @@ -22,6 +22,8 @@ #![cfg_attr(all(test, feature = "clippy"), allow(option_unwrap_used, result_unwrap_used))] extern crate byteorder; +#[macro_use] +extern crate log; #[macro_use] mod macros; diff --git a/diesel/src/sqlite/connection/raw.rs b/diesel/src/sqlite/connection/raw.rs index 2c9686f5f0e3..b2ff62f15772 100644 --- a/diesel/src/sqlite/connection/raw.rs +++ b/diesel/src/sqlite/connection/raw.rs @@ -22,9 +22,19 @@ impl RawConnection { }; match connection_status { - ffi::SQLITE_OK => Ok(RawConnection { - internal_connection: conn_pointer, - }), + ffi::SQLITE_OK => { + let r = unsafe { + ffi::sqlite3_busy_timeout(conn_pointer, 5000) + }; + + if r != ffi::SQLITE_OK { + warn!("sqlite3_busy_timeout error: {:?}", r); + } + + Ok(RawConnection { + internal_connection: conn_pointer, + }) + }, err_code => { let message = super::error_message(err_code); Err(ConnectionError::BadConnection(message.into())) From bf9acb210a4b6fed524947e122e0d4ac91fc67de Mon Sep 17 00:00:00 2001 From: king6cong Date: Fri, 2 Jun 2017 17:37:07 +0800 Subject: [PATCH 2/2] `step` should not panic when SQLITE_BUSY encountered this commit only logs the error, but can more refine grained method should be used. reference: https://github.com/jgallagher/rusqlite/blob/master/src/row.rs#L43 --- diesel/Cargo.toml | 1 - diesel/src/lib.rs | 2 -- diesel/src/sqlite/connection/raw.rs | 23 +++++++++-------------- diesel/src/sqlite/connection/stmt.rs | 2 ++ 4 files changed, 11 insertions(+), 17 deletions(-) diff --git a/diesel/Cargo.toml b/diesel/Cargo.toml index a2a0b1fd4e01..4b606a7884a8 100644 --- a/diesel/Cargo.toml +++ b/diesel/Cargo.toml @@ -25,7 +25,6 @@ time = { version = "0.1", optional = true } url = { version = "1.4.0", optional = true } uuid = { version = ">=0.2.0, <0.6.0", optional = true, features = ["use_std"] } ipnetwork = { version = "0.12.2", optional = true } -log = "0.3" [dev-dependencies] cfg-if = "0.1.0" diff --git a/diesel/src/lib.rs b/diesel/src/lib.rs index 0507ddd8b5b2..2d0645897036 100644 --- a/diesel/src/lib.rs +++ b/diesel/src/lib.rs @@ -22,8 +22,6 @@ #![cfg_attr(all(test, feature = "clippy"), allow(option_unwrap_used, result_unwrap_used))] extern crate byteorder; -#[macro_use] -extern crate log; #[macro_use] mod macros; diff --git a/diesel/src/sqlite/connection/raw.rs b/diesel/src/sqlite/connection/raw.rs index b2ff62f15772..cdbf55072b25 100644 --- a/diesel/src/sqlite/connection/raw.rs +++ b/diesel/src/sqlite/connection/raw.rs @@ -13,28 +13,23 @@ pub struct RawConnection { pub internal_connection: *mut ffi::sqlite3, } +const BUSY_TIMEOUT: i32 = 5000; + impl RawConnection { pub fn establish(database_url: &str) -> ConnectionResult { let mut conn_pointer = ptr::null_mut(); let database_url = try!(CString::new(database_url)); let connection_status = unsafe { - ffi::sqlite3_open(database_url.as_ptr(), &mut conn_pointer) + match ffi::sqlite3_open(database_url.as_ptr(), &mut conn_pointer) { + ffi::SQLITE_OK => ffi::sqlite3_busy_timeout(conn_pointer, BUSY_TIMEOUT), + err_code => err_code, + } }; match connection_status { - ffi::SQLITE_OK => { - let r = unsafe { - ffi::sqlite3_busy_timeout(conn_pointer, 5000) - }; - - if r != ffi::SQLITE_OK { - warn!("sqlite3_busy_timeout error: {:?}", r); - } - - Ok(RawConnection { - internal_connection: conn_pointer, - }) - }, + ffi::SQLITE_OK => Ok(RawConnection { + internal_connection: conn_pointer, + }), err_code => { let message = super::error_message(err_code); Err(ConnectionError::BadConnection(message.into())) diff --git a/diesel/src/sqlite/connection/stmt.rs b/diesel/src/sqlite/connection/stmt.rs index 7686ed03cede..940e4a2749b2 100644 --- a/diesel/src/sqlite/connection/stmt.rs +++ b/diesel/src/sqlite/connection/stmt.rs @@ -123,6 +123,8 @@ impl Statement { match unsafe { ffi::sqlite3_step(self.inner_statement) } { ffi::SQLITE_DONE => None, ffi::SQLITE_ROW => Some(SqliteRow::new(self.inner_statement)), + // TODO: better error handling + ffi::SQLITE_BUSY => None, error => panic!("{}", super::error_message(error)), } }