PDA

View Full Version : SQL module and Thread-safe



banita
28th July 2010, 22:52
I have database object in my mainthread (GUI).

I must do a lot of insert statement so I want to do this in other thread.
Can I create QSqlQuery in other(not main) thread and execute a lot of inserts?
QSqlQuery use database object which is created in main thread.
In QT doc is write that qt database object should not be used in other thread, so how can I start transaction in other thread?


I cant create second connection object in second thread because I use firebird embedded database.


sory for my poor english

NoRulez
29th July 2010, 09:44
I'am interessted in this too.

tbscope
29th July 2010, 09:57
http://doc.qt.nokia.com/4.6/threads-modules.html#threads-and-the-sql-module

You can use the sql module in a different thread than your main GUI thread.
BUT!!! You have to keep everything in one thread. You can not create the database in thread 1 and do a query in thread 2.

banita
29th July 2010, 15:27
so when I use firebird embedded I cant write mutlithread program.

tbscope
29th July 2010, 15:41
Multithreaded program: yes (you can have a database manager in a separate thread)
Using multiple threads for the sql classes: no

There's no reason to do that either since most databases are optimized to work in a certain way.

If you want to add lots of data at the same time in a database, don't do this query by query. Start a transaction, fill the transaction buffer and then commit it in one go.

banita
11th August 2010, 17:48
Firebird team release 2.5RC3 version.
In release note we can read:

"The client libraries, including the embedded one, can now be used in multi-threaded applications without any application-level synchronization."

so with this library can I use Qt sql classes (with one connection) in more than one thread?

tbscope
12th August 2010, 06:30
No.

This has nothing to do with the used database, but everything with Qt.