Results 1 to 5 of 5

Thread: bad mySQL host freezes app on connection

  1. #1
    Join Date
    Oct 2008
    Posts
    4
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default bad mySQL host freezes app on connection

    My little test app connects just fine to a valid database.

    If I change the variables so that I am connecting to "microsoft.com" (for example), I get the expected error message.

    If I connect to a valid host with invalid credentials, I get the expected error.

    But when I connect to some mySQL databases, my app just freezes while waiting for the connection. No message is ever returned because the app just freezes entirely - forever.

    Is there any way to timeout the db.open() or db.isvalid()? Or is there some way to use processevents() to decide that it's time to terminate the attempt?

    Qt Code:
    1. bool MainWindowImpl::createConnection()
    2. {
    3. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    4. db.setHostName("microsoft.com");
    5. db.setDatabaseName("thedata");
    6. db.setUserName("bill");
    7. db.setPassword("billybob");
    8.  
    9. if (!db.isValid()){
    10. QMessageBox::critical(0,QObject::tr("Database is not valid"),"I can't open the damned thing.");
    11. return false;
    12. }
    13. else{
    14. if (!db.open()) {
    15. QMessageBox::critical(0, QObject::tr("Database Error"),db.lastError().text());
    16. return false;
    17. }
    18. QMessageBox::information(0, QObject::tr("Database Opened Just Fine"),"apparently the username/password was valid");
    19. return true;
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Innsbruck, Austria
    Posts
    62
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: bad mySQL host freezes app on connection

    It's better to perform database operations in another thread so that the GUI doesn't freeze while connecting or performing queries. That also gives you the chance to abort the thread if the user wants to abort the operation and the server doesn't answer.

    If you do so, just remember one very important thing: don't use QtSql classes in different threads but only in the same thread. Otherwise you might get unexpected crashes due to some database engines not being thread-safe.

    Anyway, answering your question, some database engines allow you to specify the timeout. Take a look at QSqlDatabase::setConnectOptions():

    • ODBC: QL_ATTR_CONNECTION_TIMEOUT
    • PostgreSQL: connect_timeout
    • DB2: SQL_ATTR_LOGIN_TIMEOUT
    Last edited by vfernandez; 12th October 2008 at 23:18.

  3. The following user says thank you to vfernandez for this useful post:

    Frozenjim (13th October 2008)

  4. #3
    Join Date
    Oct 2008
    Posts
    4
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: bad mySQL host freezes app on connection

    Yeah, I was sort of afraid that the answer was threads. I am not an inexperienced developer, but I am brand new to C++ and QT in particular.

    OK. I'll make threads my next priority. I knew I'd have to go there sometime - might as well be tonight.

    I am quite surprised to see that mySQL doesn't have a connection_timeout option like Postgress.

  5. #4
    Join Date
    Jan 2006
    Location
    Innsbruck, Austria
    Posts
    62
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: bad mySQL host freezes app on connection

    Quote Originally Posted by Frozenjim View Post
    I am quite surprised to see that mySQL doesn't have a connection_timeout option like Postgress.
    Well, it's not the only thing it doesn't have, that's why we switched to PostgreSQL as our main DBMS. What I miss the most are notifications. They are very useful to let your application be aware of database changes without needing to poll. Qt supports them very well since 4.4.

  6. #5
    Join Date
    Jan 2006
    Location
    Innsbruck, Austria
    Posts
    62
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: bad mySQL host freezes app on connection

    About using threads for the database I suggest you to run the Qt loop in a thread using exec() and just create slots that perform the different queries, using queued connections. That way you don't need to use wait conditions, you can connect signals to slots in your thread and you can receive database notifications if the database driver supports them.

Similar Threads

  1. MySQL and shared-memory connection
    By patrikd in forum Qt Programming
    Replies: 1
    Last Post: 1st August 2008, 16:53
  2. Client/Server Error: BadIDChoice
    By 3nc31 in forum Qt Programming
    Replies: 5
    Last Post: 27th November 2007, 10:22
  3. Replies: 3
    Last Post: 2nd August 2007, 21:28
  4. Qt and MySQL Database Connection
    By shamik in forum Qt Programming
    Replies: 41
    Last Post: 6th October 2006, 12:48

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.