我已经阅读了很多关于在 android 中使用标准 SQLiteDatabase 的内容,似乎最好的方法是保留一个 SQLiteOpenHelper 实例,并且永远不要关闭返回的 SQLiteDatabase 对象,也不要关闭 SQLiteOpenHelper。问题是这些准则在使用 SQCipher 加密库时是否有效?似乎 SQCipher 包中 SQLiteOpenHelper 的 close() 方法不是空的,并且确实释放了一些东西。从不调用此方法是否安全?
下面是来自 Github ( http://goo.gl/u4L0C )的 SQLiteDatabase.close() 方法的实际代码:
public void close() {
if (!isOpen()) {
return; // already closed
}
lock();
try {
closeClosable();
// close this database instance - regardless of its reference count value
onAllReferencesReleased();
} finally {
unlock();
}
}
private void closeClosable() {
/* deallocate all compiled sql statement objects from mCompiledQueries cache.
* this should be done before de-referencing all {@link SQLiteClosable} objects
* from this database object because calling
* {@link SQLiteClosable#onAllReferencesReleasedFromContainer()} could cause the database
* to be closed. sqlite doesn't let a database close if there are
* any unfinalized statements - such as the compiled-sql objects in mCompiledQueries.
*/
deallocCachedSqlStatements();
Iterator<Map.Entry<SQLiteClosable, Object>> iter = mPrograms.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<SQLiteClosable, Object> entry = iter.next();
SQLiteClosable program = entry.getKey();
if (program != null) {
program.onAllReferencesReleasedFromContainer();
}
}
}