PDA

View Full Version : A multithreading question



sepehr
1st February 2009, 15:49
I wanted to know if QSqlQuery.exec() is atomic or not? I mean suppose I have a query which has to bring thousands of records,so it takes some time,can I leave the the thread which has the QSqlQuery.exec() and return to it when it has brought all the records?

GiuseppeBonfa
1st February 2009, 16:37
Hi,

I suggest you to take a look here:

http://lists.trolltech.com/qt-interest/2004-11/thread00370-0.html

and here:
http://www.potu.com/man/doc.trolltech.com/4.0/qtsql.html

in particular:


QSqlDatabase::database().transaction();
QSqlQuery query;
query.exec("SELECT id FROM employee WHERE name = 'Torild Halvorsen'");
if (query.next()) {
int employeeId = query.value(0).toInt();
query.exec("INSERT INTO project (id, name, ownerid) "
"VALUES (201, 'Manhattan Project', "
+ QString::number(employeeId) + ")");
}
QSqlDatabase::database().commit();


Transactions can be used to ensure that a complex operation is atomic (for example, looking up a foreign key and creating a record), or to provide a means of canceling a complex change in the middle.

Regards,
Giuseppe 'Evilcry' Bonfa'

wysota
1st February 2009, 17:04
QSqlQuery::exec() is blocking (synchronous) but not atomic, the OS can interrupt the thread and run another thread inbetween. Note that you shouldn't share a single database connection across threads anyway, so there shouldn't be problems.

sepehr
1st February 2009, 17:43
Note that you shouldn't share a single database connection across threads anyway.
I'm planning to access 5 tables in my database with relatively huge data,do you think that's a good idea to establish 5 connections?I mean is it normal?

wysota
1st February 2009, 19:59
It depends what you want to do. If you want to do that from 5 threads then you need 5 connections. If you want to do that from a single thread then one connection should be sufficient. Unless of course you will be accessing the database constantly - then the database might not send data fast enough, especially if it is on a remote machine. Then you might want to use more than one connection but this doesn't mean you will be able to process the data faster, it depends on the database - if it has to lock a table or the whole database, all other connections will have to wait until the lock is opened so probably the whole application would work even slower than with one connection.

sepehr
2nd February 2009, 06:13
if it has to lock a table or the whole database, all other connections will have to wait until the lock is opened so probably the whole application would work even slower than with one connection.
since 5 connections are trying to work on 5 different tables,if a connection doesn't lock the whole database I wouldn't have any problems,but if one connection locks the whole database I would have performance issues,could you tell me when a connection locks the whole database?I mean are there certain tasks that need to lock the whole database?what are those?or it's some kind of setting that should be configured in mysql or the connection made by qt?

wysota
2nd February 2009, 09:06
since 5 connections are trying to work on 5 different tables,if a connection doesn't lock the whole database I wouldn't have any problems,
What if you have more than one client?


but if one connection locks the whole database I would have performance issues,could you tell me when a connection locks the whole database?
I'm sorry but this is galaxies away from the scope of this forum :) It all depends on the database you use so it's best to ask at a forum related to proper database backend. I'd say transactions might cause database locks but this is just a guess.