在SQLite中并发读/写和读的策略 [英] Strategies for concurrent read/writing and reading in SQLite

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

问题描述

我有一个SQLite数据库,我保持打开和写入进程A.我想能够使用它从进程B在只读的基础上。

I have an SQLite database that I am keeping open and writing to in process A. I would like to be able to use it from process B on a read-only basis.

根据文档


  • 如果数据库为UNLOCKED,数据库可能无法读取(或写入) - 不合适

  • 如果数据库是SHARED,那么两个进程可以读取它,但第一个不能写不适合

  • 如果进程想写它需要一个EXCLUSIVE锁这意味着没有其他进程可以写不适合

  • if the database is UNLOCKED the database may not be read (or written) - unsuitable
  • if the database is SHARED then two processes can read it but the first can't write - unsuitable
  • if a process wants to write it needs an EXCLUSIVE lock which means no other processes can write - unsuitable

进程A将进行大量的小写操作我不认为对每个事务提交一个副本将是高效的。

The process A will be making lots of little writes so I don't think making a copy on each transaction commit will be efficient.

我可以看到的唯一方法是让读者等待数据库进入UNLOCKED状态,在读取期间获得SHARED锁,然后释放它。同时,进程A将要写入,并将被阻塞,直到锁变得可用 - 如果它有的话(如果进程B崩溃了怎么办?)。这意味着进程A和进程B将在锁的争用 - B想要SHARED和A想要EXCLUSIVE,这将减慢事情或甚至导致并发问题。

The only way I can see it is for the reader to wait until the database enters UNLOCKED state, get a SHARED lock for the duration of the read and then release it. Meanwhile process A will want to write and will be blocked until the lock becomes available - if it ever does (what if process B crashes?). This means that process A and process B will be in contention for locks - B wants SHARED and A wants EXCLUSIVE and this will slow things down or even lead to concurrency problems.

有没有办法实现我的并行写作和阅读的目标?

Is there any way to achieve my aim of concurrent writing and reading?

推荐答案

使用 WAL模式< a>。它支持并发阅读器和一个作者。

Use WAL mode. It supports concurrent readers and one writer.

这篇关于在SQLite中并发读/写和读的策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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