【问题标题】:How thread safe is EnableWriteAheadLogging in the context of real usage and SQLite documentation?在实际使用和 SQLite 文档的上下文中,EnableWriteAheadLogging 的线程安全性如何?
【发布时间】:2015-03-16 05:38:22
【问题描述】:

android 文档在此处描述了“EnableWriteAheadLogging”:

http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#enableWriteAheadLogging()

“此方法可以在同一个数据库上并行执行来自多个线程的查询”

但是,根据 SQLite 文档:https://www.sqlite.org/threadsafe.html

SQLite 有两种多线程:“序列化”和“多线程”。使用“EnableWriteAheadLogging”时使用哪一个?

此外,如果我的应用程序和后台服务都需要访问我的数据库,那么使用 EnableWriteAheadLogging 是否有帮助?我应该采取哪些措施来确保这可以以线程安全的方式完成?

【问题讨论】:

    标签: android sqlite android-sqlite


    【解决方案1】:

    这与线程安全无关。

    在 WAL 模式下,写入器不会阻塞读取器,因此 Android 框架认为在这种情况下使用更大的连接池是个好主意。

    也许不是,正如this comment 所示:

    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;
        }
    }
    

    【讨论】:

    • 感谢您的解释。所以调用 enableWriteAheadLogging 对sqlite.org/threadsafe.html 描述的线程安全模式没有任何影响。 sqlite 链接描述了三种不同的模式。有什么方法可以改变Android中的线程安全模式吗?
    • 在Android中,SQLite库是用SQLITE_THREADSAFE=1编译的。
    猜你喜欢
    • 2014-08-10
    • 1970-01-01
    • 2011-04-06
    • 2020-09-10
    • 2011-02-06
    • 2010-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多