How thread safe is EnableWriteAheadLogging in the context of real usage and SQLite documentation?

試著忘記壹切 提交于 2019-12-12 02:46:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!