From ffe605150afecf0bbdffb836b62a1ba68257b091 Mon Sep 17 00:00:00 2001 From: gwenn Date: Fri, 4 Nov 2016 20:47:28 +0100 Subject: [PATCH 1/2] Ensure cache is flushed when closing the connection Fix #186 --- Cargo.toml | 2 +- src/cache.rs | 17 +++++++++++++++++ src/lib.rs | 1 + 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index c5a156b..df88455 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,7 @@ trace = [] [dependencies] time = "~0.1.0" bitflags = "0.7" -lru-cache = "0.0.7" +lru-cache = "0.1.0" libc = "~0.2" chrono = { version = "~0.2", optional = true } serde_json = { version = "0.6", optional = true } diff --git a/src/cache.rs b/src/cache.rs index d246e8e..cba4e75 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -44,6 +44,10 @@ impl Connection { pub fn set_prepared_statement_cache_capacity(&self, capacity: usize) { self.cache.set_capacity(capacity) } + + pub fn flush_prepared_statement_cache(&self) { + self.cache.flush() + } } /// Prepared statements LRU cache. @@ -133,6 +137,11 @@ impl StatementCache { let sql = String::from_utf8_lossy(stmt.sql().to_bytes()).to_string(); cache.insert(sql, stmt); } + + fn flush(&self) { + let mut cache = self.0.borrow_mut(); + cache.clear() + } } #[cfg(test)] @@ -268,4 +277,12 @@ mod test { .unwrap()); } } + + #[test] + fn test_connection_close() { + let conn = Connection::open_in_memory().unwrap(); + conn.prepare_cached("SELECT * FROM sqlite_master;").unwrap(); + + conn.close().expect("connection not closed"); + } } diff --git a/src/lib.rs b/src/lib.rs index 50cf7e9..4a9d53b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -405,6 +405,7 @@ impl Connection { /// /// Will return `Err` if the underlying SQLite call fails. pub fn close(self) -> Result<()> { + self.flush_prepared_statement_cache(); let mut db = self.db.borrow_mut(); db.close() } From 674419e080d09dfdc5d1fbc32b2c3a1870b640e3 Mon Sep 17 00:00:00 2001 From: John Gallagher Date: Fri, 30 Dec 2016 23:58:12 -0500 Subject: [PATCH 2/2] Add bugfix note to Changelog --- Changelog.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Changelog.md b/Changelog.md index 9505465..7e1d231 100644 --- a/Changelog.md +++ b/Changelog.md @@ -11,6 +11,8 @@ https://github.com/jgallagher/rusqlite/pull/184. * Added `#[deprecated(since = "...", note = "...")]` flags (new in Rust 1.9 for libraries) to all deprecated APIs. +* Fixed a bug where using cached prepared statements resulted in attempting to close a connection + failing with `DatabaseBusy`; see https://github.com/jgallagher/rusqlite/issues/186. # Version 0.7.3 (2016-06-01)