>Hi,I looked at a few of the multithreading docs and posts and it looks
>like SQLite3 supports reads and writes from multiple threads provided 
>SQLite3 is compile with THREADSAFE option. 

The SQLite code is threadsafe.  It is singly entrant on a per connection basis. 
 The three THREADSAFE options invoke various levels of protection for 
programmers who do not understand multithreaded programming.

THREADSAFE=0 disables all thread checks.  It is intended where the library will 
only be used from a single thread.  Ever.  THe same thread.  Always.

THREADSAFE=1 enables SERIALIZATION within the SQLite library itself to prevent 
programmers from violating the single-entrancy rule per connection.  If you 
attempt multiple entrances, then one will succeed and the others will go into a 
scheduler WAIT until the first one is completed.  THey will then proceed one 
after another until they are all complete, with only one ever executing at a 
time, per connection.

THREADSAFE=2 disables the mutexes used for SERIALIZATION.  If you violate the 
single entrancy per connection requirement then it is likely that you will 
experience the rapture.

>In this case each thread has it own DB connection. 

In which case?

>My question is if I have a single DB connection which is used
>by multiple threads then are simultaneous reads and writes supported 
>from these threads. 

Yes.  But only one thread per connection will be permitted to execute code 
within the library.  With THREADSAFE=1 the library itself will ensure this is 
enforced.  With THREADSAFE=2 you must enforce this requirement.

>I understand writes will lock the DB but other than this will
>this work. 

Yes.   However all operations on a single connection operate on a single 
connection.  That is, there is ONE transaction context per connection and 
"multiple threads" are irrelevant to this fact.  On a single connection 
requests are MULTI-LEAVED.  Context is by connection, not by statement or 
thread.

>Basically are there issues if a read is happening on the DB
>connection can another read happen in the same DB connection from another 
>thread. 

Define "issues".  Generally no.

>On the same thread this is not an issue. I was wondering whether there could
>be issues when doing from multiple threads.

No.  Only one thread is executing within the library at a time, per connection. 
 There is no difference between multiple threads and multi-leaving the same 
sequence of requests on a single thread.

>The reason I am sharing a single
>connection with multiple threads is because I need to listen to the DB
>change notification. Currently SQLite3 supports listening to
>notifications
>from a single connection only. For this reason I am maintaining a single
>connection shared by multiple threads.ThanksPrashant




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

Reply via email to