Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSqlDatabase connection issue
Forum Updated to NodeBB v4.3 + New Features

QSqlDatabase connection issue

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 1.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Cobra91151C Offline
    Cobra91151C Offline
    Cobra91151
    wrote on last edited by Cobra91151
    #1

    Hi! I have the application witch requires connection to Database on the server. It all works well but after 4 minutes of inactivity of DB connection it displays errors.

    0_1529744896243_connection_error.jpg

    I know the problem is with wait_timeout variable on DB server but I can't change it because I don't have permission to the server. On the server it has value of 240 seconds = 4 minutes.

    I have added some check to execute query and it will check connection:

    Code:

    void Test::dbTest()
    {
        QSqlQuery sqlQuery("some query", QSqlDatabase::database("ConnectionName"));
        sqlQuery.setForwardOnly(true);
    
        if (sqlQuery.size() > 0) {
           // Some action
        } else {
            int errorCode = sqlQuery.lastError().number();
    
            if (errorCode == 2013) {
                QSqlDatabase::database("ConnectionName").open(); // opens DB connection
                dbTest(); // runs recursively
            } else {
                qDebug() << sqlQuery.lastError().text();
            }
        }
    }
    

    It fixes such situation but I need to add such check for all functions that works with DB. But what if I can omit such check and everywhere just run QSqlDatabase::open() before query? What will happen when connection is still alive and QSqlDatabase::open() function will run again, it will create another connection or use existing? Thanks.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What about using a heartbeat on that connection ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      Cobra91151C 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        What about using a heartbeat on that connection ?

        Cobra91151C Offline
        Cobra91151C Offline
        Cobra91151
        wrote on last edited by
        #3

        @SGaist

        Yes, it also will work. But I have checked QSqlDatabase::open() solution on WAMP and it creates only 1 connection to DB. Thanks.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You should take a look at the QSqlDatabase::setConnectOptions and the specifics for MySQL. You could fine tune the timeout for your server.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          Cobra91151C 1 Reply Last reply
          1
          • SGaistS SGaist

            You should take a look at the QSqlDatabase::setConnectOptions and the specifics for MySQL. You could fine tune the timeout for your server.

            Cobra91151C Offline
            Cobra91151C Offline
            Cobra91151
            wrote on last edited by
            #5

            @SGaist

            I have added setConnectOptions("MYSQL_OPT_RECONNECT=TRUE"); and now it automatically reconnects. Thanks.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved