My application manages all its data in a a SQLiteDatabase that is accessed by a number of threads.
Right now I’ve been keeping all my DB calls synchronized on the database itself.
The reason I want to do this is that occasionally I want to refresh the table by grabbing the most recent version from a server and reconstructing the table from stratch. To save time I’m actually making a second table and then replacing the original when I’m done (locking it out with a synchronized while I’m doing this).
The problem is either I occasionally have my SQL calls stall for a long while (due to the synchronous locks) or I get an error when a SQL call tries to run itself in the brief period when my one table is getting copied over.
Is there a was for me to lock my database from other operations while it refreshes but let operations A,B,C,etc run concurrently?
Cheers!
I’m pretty sure what you want is a ReadWriteLock. Basically, it puts two different locks on your database, one for reading and one for writing. While the write lock is locked, nobody else can read or write. If any of the read lock is locked (by any number of threads), the write lock won’t be able to lock until everyone is finished reading.