Results 1 to 2 of 2

Thread: Detecting QSqlDatabase disconnections

  1. #1
    Join Date
    Dec 2010
    Posts
    18
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Detecting QSqlDatabase disconnections

    I'm working with both QMYSQL and QODBC drivers. Both drivers work fine as long as the database is working. If the database server goes offline, QSqlDatabase fails to detect it, and all the calls to isOpen() keep returning "true", no matter that all the queries are failing.

    As a workaround, I'm subclassing QSqlQuery, to rewrite exec() so that if the query fails, and the error number corresponds to a disconnected database (this is driver-dependant, as QSqlError::type is bullshit and always returns QSqlError::StatementError, instead of QSqlError::ConnectionError ). However I see no way for accessing the QSqlDatabase that was passed to the QSqlQuery constructor, so that I can close it, and driver().close() doesn't work either because "driver()" is const. It's weird that there's no database() method in QSqlQuery, I hope it's for a good reason, but I guess I'll have to store my own m_database if I want to make things work.

    I bet someone here must have dealt with this problem before, as detecting when your database goes offline seems to me as a very basic feature (that Qt should implement). Am I going in the wrong direction? Does anyone know of a better solution? Mine just feels overcomplicated for such a basic feature.

  2. #2
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Detecting QSqlDatabase disconnections

    QSqlDatabase doesn't know because it is not told so by database when it is about to die. To know whether alive or not you can send something like select 1 (from table) query timely or before you intend to issue your query.

Similar Threads

  1. QT3 - detecting peer
    By sinja in forum Qt Programming
    Replies: 2
    Last Post: 7th October 2009, 17:44
  2. Detecting OS
    By ankit17.ag in forum Qt Programming
    Replies: 2
    Last Post: 16th July 2008, 10:10
  3. detecting disks
    By nishad_kumar in forum Qt Programming
    Replies: 3
    Last Post: 7th September 2007, 07:35
  4. Detecting low memory
    By steg90 in forum General Programming
    Replies: 3
    Last Post: 11th June 2007, 18:54
  5. detecting Hardware?!?!
    By nupul in forum Newbie
    Replies: 2
    Last Post: 7th April 2006, 08:50

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
  •  
Qt is a trademark of The Qt Company.