Ensure cache is flushed when closing the connection

Fix #186
This commit is contained in:
gwenn 2016-11-04 20:47:28 +01:00
parent bf8c495b69
commit ffe605150a
3 changed files with 19 additions and 1 deletions

View File

@ -22,7 +22,7 @@ trace = []
[dependencies] [dependencies]
time = "~0.1.0" time = "~0.1.0"
bitflags = "0.7" bitflags = "0.7"
lru-cache = "0.0.7" lru-cache = "0.1.0"
libc = "~0.2" libc = "~0.2"
chrono = { version = "~0.2", optional = true } chrono = { version = "~0.2", optional = true }
serde_json = { version = "0.6", optional = true } serde_json = { version = "0.6", optional = true }

View File

@ -44,6 +44,10 @@ impl Connection {
pub fn set_prepared_statement_cache_capacity(&self, capacity: usize) { pub fn set_prepared_statement_cache_capacity(&self, capacity: usize) {
self.cache.set_capacity(capacity) self.cache.set_capacity(capacity)
} }
pub fn flush_prepared_statement_cache(&self) {
self.cache.flush()
}
} }
/// Prepared statements LRU cache. /// Prepared statements LRU cache.
@ -133,6 +137,11 @@ impl StatementCache {
let sql = String::from_utf8_lossy(stmt.sql().to_bytes()).to_string(); let sql = String::from_utf8_lossy(stmt.sql().to_bytes()).to_string();
cache.insert(sql, stmt); cache.insert(sql, stmt);
} }
fn flush(&self) {
let mut cache = self.0.borrow_mut();
cache.clear()
}
} }
#[cfg(test)] #[cfg(test)]
@ -268,4 +277,12 @@ mod test {
.unwrap()); .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");
}
} }

View File

@ -405,6 +405,7 @@ impl Connection {
/// ///
/// Will return `Err` if the underlying SQLite call fails. /// Will return `Err` if the underlying SQLite call fails.
pub fn close(self) -> Result<()> { pub fn close(self) -> Result<()> {
self.flush_prepared_statement_cache();
let mut db = self.db.borrow_mut(); let mut db = self.db.borrow_mut();
db.close() db.close()
} }