On 24 May 2011, at 12:43pm, John Deal wrote:

> Sorry to be so brain-dead but I am still confused.  I have multiple threads, 
> each with their own DB connection.

Read

http://www.sqlite.org/threadsafe.html

If it's not clear to you please ask specific questions about what's on that 
page, since your questions will help us to work out how to improve it.

> I want to allow multiple readers accessing the DB at the same time since 
> nothing is changing.  However, if a writes is to take place, I want all 
> readers to finish their reads and give the writer exclusive access.  Once the 
> writer is done, the readers can come back in.
> 
> I have all writes in transactions.  If I deactivate my pthread_rwlock() that 
> enforce the above, several writes fail with a "database locked" error (I 
> assume it is returning SQLITE_BUSY).

The documentation on how to handle this is a little lacking, but if your app 
has no principles of its own, just loop until the problem goes away.

> With my pthread_rwlock(), I have multiple threads reading the DB and my 
> writes get the exclusive access they need.  Now I could loop on the write 
> until it gets in but that seems very wasteful.
> 
> So how do I implement the equivalent of a pthread_rwlock() using SQLite 
> mechinisms?

You use transactions correctly.  Pay especial attention to the differences 
between

BEGIN
BEGIN IMMEDIATE
BEGIN EXCLUSIVE
BEGIN DEFERRED

See the following page

http://www.sqlite.org/lang_transaction.html

However, you may have done something to defeat SQLite's built in mechanism.  
Have you done any of the following:

Have you set any of SQLite's compiler directives in your compilation ?  I'm 
thinking especially of SQLITE_THREADSAFE.  I'm not completely familiar with 
SQLite's model, but it seems to me as if you should be using 
'-DSQLITE_THREADSAFE=2'.  The explanation of this is on the first web page I 
pointed to above.

Have you set any PRAGMAs which are related to threading ?  Especially 'PRAGMA 
temp_store_directory' and 'PRAGMA read_uncommitted' ?   If so, try removing 
your PRAGMAs and see if this fixes your problem.

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

Reply via email to