Re: [Cppcms-users] Threaded access to SQLite
Brought to you by:
artyom-beilis
From: Artyom B. <art...@gm...> - 2017-01-13 20:17:43
|
On Thu, Jan 12, 2017 at 6:41 PM, Josh Hunsaker <jo...@ni...> wrote: > Artyom Beilis wrote: >>> I have created a stand-alone test that shows the difference between >>> sharing a single SQLite database connection between threads, and >>> creating a new connection for each thread. It can be viewed here: >>> >>> https://gist.github.com/nispio/fe2955c9a0978532cf7b864a8b1b54db >>> >> >> >> Actually it shows exact behaviour I expected >> >> This is my run (note on PC with 4 cores): >> >> >> SAME CONNECTION >> Thread 1: library routine called out of sequence >> Thread 2: library routine called out of sequence >> Thread 3: library routine called out of sequence >> FAILED run(insert_random, "/var/tmp/testdb1.db") >> NEW CONNECTION >> Thread 2: database is locked >> Thread 3: database is locked >> FAILED run(new_connection_insert_random, "/var/tmp/testdb2.db") >> > > I'm sorry. I made a mistake when posting to Gist. I made a slight tweak > to the code when posting, and forgot to check if it worked. (Rookie > mistake, I know). The error that you are seeing in the first case is > not related to threading at all; it is just a bad pointer. Will you please > humor me and try one more time with the updated code? > > https://gist.github.com/nispio/fe2955c9a0978532cf7b864a8b1b54db > It does not change the fact that the code is wrong. There is meaning of the session/connection - you can't use it simultaneously from multiple threads even if it is allowed by API, For example how do you run concurrent transactions from different threads on same connection? You can't safely (in terms of logical sessions) use same connection from multiple threads. And you must deal with DB locked error and be able to recover from it. Regards, Artyom |