PDA

View Full Version : sqlite in multithreaded environment - how to?



TorAn
26th February 2010, 18:32
My application has several threads. Each thread opens sqlite database with the call
QSqlDatabase _db = QSqlDatabase::addDatabase("QSQLITE");

, does some work with queries and closes the connection with _db.close();

"SQLite And Multiple Threads" (http://www.sqlite.org/threadsafe.html) chapter of sqlite documentation states that there are three modes that sqlite can be used with:
1.Single-thread,2.Multi-thread and 3.Serialized. In serialized mode, SQLite can be safely used by multiple threads with no restriction.

So, "serialize" mode is what I have to use, because right now, single thread works just fine, but when there is more then one thread my work with database produces errors.

My questions are:
1. I complied 4.6.2 without specifying any flags for sqlite driver. What is the mode that my sqlite driver is after such compilation and is it possible to find it programmatically?
2. How to set up "Serialized" mode for using with sqlite database? Code sample will be much appreciated.
3. When several threads are opening/closing database connection - what is the right way to do this? SqlServer practice defines opening db connection as an inexpensive operation and avises against caching connections. Is that also true for sqlite?
4. Is it necessary to explicitly close the connection or the driver closes it automatically when QSqlDatabase goes out of scope?
5. SQLite in multithreaded application with Qt - any programming sample or article that show "Qt" way of doing it will be appreciated.