Results 1 to 8 of 8

Thread: how to reconnect CORRECTLY qmysql database?

  1. #1
    Join Date
    Dec 2009
    Posts
    15
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Question how to reconnect CORRECTLY qmysql database?

    Hi all,

    I've installed qt-sdk-win-opensource-2009.05.exe and then wrote a simple program to test whether QMYSQL plugin was successfully built and connected. As of now there is no problem but I need mysql_ping functionality. How to try to reconnect correctly if isOpen function returns false?

    I've attached a part of code. Before executing a query, I want to test connectivity and reconnect if it's broken. In order to accomplish this, must removeDatabase be firstly called? Without definition of another a QSqlDatabase instance, can existing qtafx_db variable be usable? I'll pleasure so much if you can clarify this matter.

    Wish you all the best,

    Qt Code:
    1. int main (int argc, char** argv)
    2. {
    3. QCoreApplication qtafx_app (argc, argv);
    4. bool qStatus;
    5. {
    6. QSqlDatabase qtafx_db = QSqlDatabase::addDatabase ("QMYSQL");
    7. qtafx_db.setHostName ("");
    8. qtafx_db.setDatabaseName ("");
    9. qtafx_db.setUserName ("");
    10. qtafx_db.setPassword ("");
    11.  
    12. qStatus = qtafx_db.open();
    13.  
    14. ....
    15. // before executing a query, I want to test connectivity and reconnect if it's broken
    16. ....
    17.  
    18. qtafx_db.close ();
    19. qtafx_db.removeDatabase ();
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 5th January 2010 at 15:09. Reason: Missing [code] tags

  2. #2
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to reconnect CORRECTLY qmysql database?

    You could try enclosing the query in an if block:

    Qt Code:
    1. if (!qtafx_db.open()){
    2.  
    3. // DB not open - create connection
    4.  
    5. }else
    6. {
    7.  
    8. // Submit query
    9. // and inspect QSqlQuery::lastError() to ensure it worked.
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Sep 2009
    Location
    Tashkent, Uzbekistan
    Posts
    107
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to reconnect CORRECTLY qmysql database?

    You can try this:

    Qt Code:
    1. int retries = 5;
    2. int delay = 1000 * 3; // time in ms
    3. QTimer delayTimer;
    4. while (!qtafx_db.open() || retries)
    5. {
    6. delayTimer.start(delay);
    7. loop.exec();
    8. retries--;
    9. }
    10.  
    11. if (!qtafx_db.isOpen())
    12. {
    13. qtafx_db.close ();
    14. qtafx_db.removeDatabase ();
    15. return 1; // Or your prefered code here or popup message
    16. }
    To copy to clipboard, switch view to plain text mode 

    In this code sample you 5 times retries to connect to the database with 3 seconds delay between retries. Hope that helps.
    -- Tanuki

    per cauda vel vaculus cauda

  4. #4
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to reconnect CORRECTLY qmysql database?

    Small point but you are closing a connection that is not open in line 14.

    Out of interest why would you want to poll a connection like this?

    If you open a connection, you may as well try to action the query.
    Test the lasterror value to ensure that the query executed properly.
    You may wish to use transactions to guarantee that the whole query was executed if your DB supports them.

    If the connection is so intermittent that it needs to be tested before each query, there is no gaurantee that the connection will still be there immediately after the test has completed.

  5. #5
    Join Date
    Sep 2009
    Location
    Tashkent, Uzbekistan
    Posts
    107
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to reconnect CORRECTLY qmysql database?

    If you mean my code snippet then please take into consideration that it shows a piece of code. I don't know the parameters
    yaseminyilmaz will use to open database. This is what's going on after you establish database connection.
    -- Tanuki

    per cauda vel vaculus cauda

  6. #6
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to reconnect CORRECTLY qmysql database?

    No, I was wondeing about the general approach of opening a connection and testing it before submitting any queries (that may still fail regardless), not your answer.

  7. #7
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to reconnect CORRECTLY qmysql database?

    I use the typical connection.h:
    Qt Code:
    1. #ifndef CONNECTION_H
    2. #define CONNECTION_H
    3.  
    4. #include <QMessageBox>
    5. #include <QSqlDatabase>
    6. #include <QSqlError>
    7. #include <QSqlQuery>
    8. static bool createConnection(const QString newHostName, const QString newDatabase, const QString newUserName, const QString newPassword){
    9. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    10. db.setHostName(newHostName);
    11. db.setDatabaseName(newDatabase);
    12. db.setUserName(newUserName);
    13. db.setPassword(newPassword);
    14. if (!db.open()) {
    15. QMessageBox::critical(0, qApp->trUtf8("ERROR!"),
    16. qApp->trUtf8("Error message\n"
    17. ""
    18. "info..."), QMessageBox::Cancel);
    19. return false;
    20. }
    21. return true;
    22. }
    23. #endif
    To copy to clipboard, switch view to plain text mode 

    One question about this.
    Every time i reconnect i get a
    Qt Code:
    1. QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
    To copy to clipboard, switch view to plain text mode 

    Is there a problem with this?

  8. #8
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to reconnect CORRECTLY qmysql database?

    It sounds as though you are trying to open multiple connections to your database without closing the previous ones.

    It is normal to reuse the already open connection rather than open a new one each time you want to access your database.

    In your case try calling the createConnection() just once at the start of the programme and submit your all queries using this.

    If you need multiple connections for some reason give them different names.

Similar Threads

  1. Threads and database connection
    By probine in forum Qt Programming
    Replies: 9
    Last Post: 7th August 2013, 08:30
  2. how to reconnect CORRECTLY qmysql database?
    By yaseminyilmaz in forum Qt Programming
    Replies: 0
    Last Post: 31st December 2009, 12:20
  3. Multiple database connections
    By cyberboy in forum Qt Programming
    Replies: 3
    Last Post: 30th March 2008, 16:56
  4. Issues regarding QMySql drivers and mysql database
    By bera82 in forum Qt Programming
    Replies: 2
    Last Post: 10th August 2006, 17: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.