PDA

View Full Version : Abort QSqlQuery::exec()



elmo
9th September 2009, 16:53
If I have a time consuming query (i.e. few minutes) how can I abort it's execution?
I want to start new QThread in which I will call QSqlQuery::exec() and I have to have the ability to restart query (stop current one, change query string and start over).

victor.fernandez
9th September 2009, 18:41
It's not possible. You would have to terminate the thread (http://doc.trolltech.com/4.5/qthread.html#terminate) execution since QSqlQuery::exec() is a blocking function. However, aborting a thread is not recommended, as the documentation states:


Warning: This function is dangerous and its use is discouraged. The thread can be terminate at any point in its code path. Threads can be terminated while modifying data. There is no chance for the thread to cleanup after itself, unlock any held mutexes, etc. In short, use this function only if absolutely necessary.

In the end, if you want to be able to safely abort a query, the only suitable way I can think of is creating a child process that performs the queries and killing it when you need to abort the query.

However, if you're not going to abort the query very often, you may try with terminate(), keeping in mind it's risky and it can produce crashes, memory leaks and so on. I'm using terminate() in my application and it's working fine but I normally don't need to call it, it's a last-resort.

elmo
9th September 2009, 19:41
However, aborting a thread is not recommended, as the documentation states: I read about terminating a thread and it's not something I'm going to consider as long as I'm sure it's safe to do (and I probably won't be with QSqlQuery::exec()).


However, if you're not going to abort the query very often, you may try with terminate() Unfortunately I'm going to kill them fairly often.


keeping in mind it's risky and it can produce crashes, memory leaks and so on. I'm using terminate() in my application and it's working fine but I normally don't need to call it, it's a last-resort.For me it's not even a last resort. For me it's unacceptable to write application in a way it allows situations in which application may crash (in theory memory leaks lead to using up all memory, so it's also a thing leading to crash).

kevin_986
13th October 2013, 13:58
you can terminate the thread, but QSqlQuery .exec continue runing.

anda_skoa
13th October 2013, 15:56
Does the database you are working with support paging?

If yes you could probably execute your query in smaller pieces.

Cheers,
_

ChrisW67
13th October 2013, 21:50
Don't forget to thoroughly investigate why your single query is taking minutes, where that time is being spent, and redesign or adapt the query and database to perform better. Better joins, better indexes, temporary tables are all candidates you investigate.