PDA

View Full Version : Database queries and Threads



zzz9
9th August 2012, 09:38
I need to write a multithreaded application doing database queries.
At this time I have chosen the following architecture:


DatabaseTools : public QObject
// wrapper class that provides programmer with an abstracted functions such as "connect" and "login"
{
DatabaseObject : public QThread, public QSqlDatabase
// provides programmer with multithreaded connect in "void run ()"

DatabaseQuery : public QThread, QSqlQuery
DatabaseQuery (DatabaseObject*, QString query);
// provides programmer with multithreaded database query in "void run ()"
}

When I use connect method of DatabaseTools (opens database connection with multithreaded QSqlDatabase::open in DatabaseObject::run()) everything works great, but when I try to do some multithreaded queries, QSqlQuery's exec() method returns false with a simple query. There is also a problem with last error (displays nothing).

I have read that there are some peculiarities with Qt Sql Module and threads.

Can you advise me a good achitecture of multithreaded database queries?

Added after 8 minutes:

I've been struck with a perfect idea, I can write multithreaded sql qeuries with a database sdk, that means that I can use database sdk in Qt classes. But I have to write another module if I change database from MySQL to PostgreSql, for example.

At the same moment, could you suggest a good architecture in the post above?

Qtonimo
9th August 2012, 09:49
You have to establish the database connection in run Method of Thread:

Look at this article http://www.linuxjournal.com/article/9602 ;)

Berna22
28th March 2013, 11:26
So we have this ultimate fail vendor (it's a long story, but trust me here) that has created an application that has separate, but identical in design, databases that we need to query (10 of them actually). Each of these databases is for a different "location" - but it's all still information relevant to all locations.

I've written some code to start 10 unique threads to issue queries to each of the separate databases, wait for the results to finish, and then combine them into a single set of records that can be used an manipulated.