From ffe605150afecf0bbdffb836b62a1ba68257b091 Mon Sep 17 00:00:00 2001 From: gwenn Date: Fri, 4 Nov 2016 20:47:28 +0100 Subject: [PATCH] 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() }