Is it possible to lock an SQLite table for the current thread?

My application manages all its data in SQLiteDatabase, which is accessed by several threads.

Currently, I support all my DB calls synchronized in the database itself.

The reason I want to do this is because sometimes I want to update the table by grabbing the latest version from the server and restoring the table from stratch. To save time, I actually do a second table and then replace the original when I finish (lock with sync while I do this).

The problem is either that sometimes SQL queries fail (due to synchronous locks), or I get an error when an SQL query tries to start itself in a short period when my single table is copied.

Do I have the opportunity to block my database from other operations while it is being updated, but do operations A, B, C, etc. allow it? work at the same time?

Hooray!

+5
source share
1 answer

I am sure you want ReadWriteLock . Basically, it puts two different locks in your database, one for reading and one for writing. Although the write lock is locked, no one can read or write. If any read lock is blocked (by any number of threads), the write lock will not be able to lock until everyone has read it.

0
source

All Articles