SQLite的同时读写 [英] SQLite simultaneous reading and writing

查看:1507
本文介绍了SQLite的同时读写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了很多话题,但无法找出答案的问题:是否有可能读取和写入的同时

I've read a lot of topics, but can't figure out answer for question: is it possible to read and write simultaneous?

我有后台线程更新一些数据和用户界面需要小块保存在数据库的数据。所以在UI线程选择操作被执行。但块时更新过程中。作为结果,UI冻结几秒钟。

I have background thread that updates some data and UI needs small piece of data stored in DB. So in UI thread SELECT operation is performed. But it blocks when update is in progress. As result, UI freezes for several seconds.

有没有人有从DB写入时读取成功?

Does anyone has success in reading from DB when writing?

它可以读取和写入数据库在iPhone上。请问这种差异的原因是在本地sqlite的功能synchronious实现包装的?

Its possible to read and write to DB on iPhone. Does the reason of such difference is in synchronious implementation of wrapper on native sqlite functions?

推荐答案

在Android 3.0及更高SQLiteDatabases支持WAL模式(预写日志):

On Android 3.0 and higher SQLiteDatabases support WAL mode (write-ahead logging):

当预写日志未启用(默认值),这不是   可能的读取和写入发生在数据库上在同一   时间。之前修改数据库,作家隐式获取到一个   其中$访问p $ pvents读者对数据库的排它锁   直到写完成数据库。

When write-ahead logging is not enabled (the default), it is not possible for reads and writes to occur on the database at the same time. Before modifying the database, the writer implicitly acquires an exclusive lock on the database which prevents readers from accessing the database until the write is completed.

在此相反,当预写日志已启用,写操作   发生在一个单独的日志文件,允许读取继续   兼任。虽然写入时,其他线程读者   将感知数据库的状态,因为它是写之前   开始了。当写操作完成后,对其他线程的读者会再   感知数据库的新状态。

In contrast, when write-ahead logging is enabled, write operations occur in a separate log file which allows reads to proceed concurrently. While a write is in progress, readers on other threads will perceive the state of the database as it was before the write began. When the write completes, readers on other threads will then perceive the new state of the database.

<一个href="http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#enableWriteAheadLogging%28%29">http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#enableWriteAheadLogging()

要启动WAL模式,而不是使用的BeginTransaction beginTransactionNonExclusive()交易()。 虽然的BeginTransaction()开始以EXCLUSIVE模式交易,beginTransactionNonExclusive()在即时模式下启动一个

To start a transaction in WAL mode use beginTransactionNonExclusive() instead of beginTransaction(). While beginTransaction() starts a transaction in EXCLUSIVE mode, beginTransactionNonExclusive() starts one in IMMEDIATE mode

  • EXCLUSIVE模式采用独家锁(<一href="http://www.sqlite.org/lockingv3.html#excl_lock">http://www.sqlite.org/lockingv3.html#excl_lock)这意味着,除了READ_UNCOMMITTED连接没有其他的数据库连接,就能读取数据库并无一例外没有其他的连接将能够编写数据库中,直到交易完成
  • 在即时模式下使用保留锁(<一href="http://www.sqlite.org/lockingv3.html#reserved_lock">http://www.sqlite.org/lockingv3.html#reserved_lock)这意味着没有其他的数据库连接将能够写入数据库或执行BEGIN IMMEDIATE或BEGIN EXCLUSIVE,其他进程可以继续从数据库中读取,但是。
  • EXCLUSIVE mode uses exclusive locks (http://www.sqlite.org/lockingv3.html#excl_lock) meaning no other database connection except for read_uncommitted connections will be able to read the database and no other connection without exception will be able to write the database until the transaction is complete
  • IMMEDIATE mode uses reserved locks (http://www.sqlite.org/lockingv3.html#reserved_lock) meaning no other database connection will be able to write to the database or do a BEGIN IMMEDIATE or BEGIN EXCLUSIVE, other processes can continue to read from the database, however.

在简单的话:拨打beginTransactionNonExclusive()为即时模式,而另一个线程写入(国家,我们可以读出写传输开始前,因为我们不会使用READ_UNCOMMITTED连接 - > <一个href="http://en.wikipedia.org/wiki/Isolation_%28database_systems%29#Dirty_reads">http://en.wikipedia.org/wiki/Isolation_%28database_systems%29#Dirty_reads).

In simpler words: call beginTransactionNonExclusive() for IMMEDIATE mode and we can read while another thread is writing (the state before the write transaction started because we won't use read_uncommitted connections -> http://en.wikipedia.org/wiki/Isolation_%28database_systems%29#Dirty_reads).

这篇关于SQLite的同时读写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆