问题
The android documentation describes "EnableWriteAheadLogging" over here:
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#enableWriteAheadLogging()
"This method enables parallel execution of queries from multiple threads on the same database"
However, according to the SQLite documentation: https://www.sqlite.org/threadsafe.html
there are two kinds of multithreading for SQLite: "Serialized" and "Mulit-thread". Which one is used when using "EnableWriteAheadLogging"?
In addition, if my database needs to be accessed by both my app and a background service, does it help to use EnableWriteAheadLogging? Which actions should I take to make sure this can be done in a thread safe way?
回答1:
This has nothing to do with thread safety.
In WAL mode, a writer does not block readers, so the Android framework thinks it is a good idea to use a larger connection pool in this case.
Or maybe not, as this comment shows:
private void setMaxConnectionPoolSizeLocked() {
if ((mConfiguration.openFlags & SQLiteDatabase.ENABLE_WRITE_AHEAD_LOGGING) != 0) {
mMaxConnectionPoolSize = SQLiteGlobal.getWALConnectionPoolSize();
} else {
// TODO: We don't actually need to restrict the connection pool size to 1
// for non-WAL databases. There might be reasons to use connection pooling
// with other journal modes. For now, enabling connection pooling and
// using WAL are the same thing in the API.
mMaxConnectionPoolSize = 1;
}
}
来源:https://stackoverflow.com/questions/29062967/how-thread-safe-is-enablewriteaheadlogging-in-the-context-of-real-usage-and-sqli