Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One SQLiteConnection per thread?

I am using SQLite from system.data.sqlite.org

We need to access the database from many threads (for various reasons). I've read a lot about sqlite thread safe capabilities (the default synchronized access mode is fine for me).

I wonder if it is possible to simply open a connection per thread. Is something like this possible? I really don't care about race conditions (request something that hasn't been inserted yet). I am only interested in the fact that it is possible to access the data using one SQLiteConnection object per thread.

like image 578
Odys Avatar asked Mar 28 '12 14:03

Odys


2 Answers

Yes. In fact, it's the proper way, as SQLite is not thread safe (by default. You can make it threadsafe compiling with some option). And just to ensure it works: SQLite is being used in some small websites, so multithreading is there :)

Here more information: http://www.sqlite.org/faq.html#q6

like image 128
Ivo Avatar answered Nov 03 '22 05:11

Ivo


Given you use a separate connection per thread you should be fine.

From docs

Note that SQLiteConnection instance is not guaranteed to be thread safe. You should avoid using the same SQLiteConnection in several threads at the same time. It is recommended to open a new connection per thread and to close it when the work is done.

like image 37
oleksii Avatar answered Nov 03 '22 06:11

oleksii