Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
685 views
in Technique[技术] by (71.8m points)

concurrency - Strategies for concurrent read/writing and reading in SQLite

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.

According to the document,

  • 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

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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...