I ran several multi-threads tests these days and I want to get a confirmation
that my understanding is correct.
I use WAL mode and I think whether or not use the same connection with
THREADSAFE=1, 2 is the key to my question. 

Mode 1, threadsafe=2 + multiple threads use the same connection:
It is not threadsafe;

Mode 2, threadsafe=2 + each thread runs a sqlite3_open():
It is threadsafe which means reading and writing can proceed concurrently,
but only one writer at a time. PRAGMA busy_timeout() may avoid “db is
locked” when writing.

Mode 3, threadsafe=1 + each thread runs a sqlite3_open():
Same with mode 2, as threadsafe=1 is only supported the ability of a handle
to be used 
by more than one thread. 

Mode 4, threadsafe=1 + multiple threads use the same connection:
Reading and writing can proceed concurrently; Two writers can start at the
same time and sqlite will make them Serialized(but how? guess some threads
will be blocked and retry, but I can not find it in the source code).

In general, WAL make reading and writing concurrent - not just serial, but
writing and writing can only be serial. So writers should use busy_timeout()
to retry(Mode 2), or, use the same connection and the RETRY operation will
be done by sqlite(Mode 4).

I think sqlite is threadsafe means the integrity of database is guaranteed.
And there will not be any crash or corruption‎ if applications use sqlite
the way like mode 2 and 4 above.

Is it right?


Sent from: http://sqlite.1065341.n5.nabble.com/
sqlite-users mailing list

Reply via email to