Results 1 to 6 of 6

Thread: Abort QSqlQuery::exec()

  1. #1
    Join Date
    Sep 2009
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Abort QSqlQuery::exec()

    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).

  2. #2
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Abort QSqlQuery::exec()

    It's not possible. You would have to terminate the thread 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.

  3. #3
    Join Date
    Sep 2009
    Posts
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Abort QSqlQuery::exec()

    Quote Originally Posted by victor.fernandez View Post
    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()).

    Quote Originally Posted by victor.fernandez View Post
    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.

    Quote Originally Posted by victor.fernandez View Post
    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).

  4. #4
    Join Date
    Oct 2013
    Posts
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Abort QSqlQuery::exec()

    you can terminate the thread, but QSqlQuery .exec continue runing.

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Abort QSqlQuery::exec()

    Does the database you are working with support paging?

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

    Cheers,
    _

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Abort QSqlQuery::exec()

    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.

Similar Threads

  1. Abort QDialog in the constructor
    By Tino in forum Qt Programming
    Replies: 2
    Last Post: 29th July 2009, 15:36
  2. Replies: 4
    Last Post: 14th May 2009, 00:43
  3. how to abort a function taking to long to process
    By john_god in forum General Programming
    Replies: 5
    Last Post: 30th April 2009, 01:06
  4. Replies: 2
    Last Post: 17th April 2009, 22:36
  5. Replies: 1
    Last Post: 9th September 2008, 00:10

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.