On 13/06/2011, at 6:49 PM, Ian Hardingham wrote:

> As was being discussed yesterday, I have four processes accessing the 
> same database file.  When they perform an sqlite action, I wish them to 
> block if the DB is not available.  SQLite does not block if it finds the 
> db busy or locked, it returns an error code.

Short answer:

Make sure each process accesses the SQLite database inside their own 
transaction. If the transaction contains a command that will change the 
database (eg delete, update or insert), then use "begin immediate" to start the 
transaction, so that it won't proceed unless it has unchallenged access to the 
database. For other transactions, which are making no changes (ie just select), 
use "begin deferred" (which is the default behavior for plain "begin").

The logic is that only one writing (update, delete or insert) transaction at a 
time will be given a reserved lock. Each writing transaction must wait their 
turn and will not start until they can have "immediate" reserved access. 
Meanwhile, multiple read-only (select) transactions can happily start and 
finish at will.

For more info on transaction types, see: 
http://www.sqlite.org/lang_transaction.html

Set the timeouts as long as is reasonable for the queue of other write 
transactions to finish.

Tom Brodhurst-Hill
BareFeetWare

 --
Comparison of SQLite GUI tools:
http://www.barefeetware.com/sqlite/compare/?ml
--
iPhone/iPad/iPod and Mac software development, specialising in databases
develo...@barefeetware.com

_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to