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 Update on Monday, May 27th 2025

QSqlDatabase connection issue

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 1.3k Views
  • 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.
  • C Offline
    C Offline
    Cobra91151
    wrote on 23 Jun 2018, 09:27 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
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 23 Jun 2018, 22:27 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

      C 1 Reply Last reply 24 Jun 2018, 05:42
      0
      • S SGaist
        23 Jun 2018, 22:27

        Hi,

        What about using a heartbeat on that connection ?

        C Offline
        C Offline
        Cobra91151
        wrote on 24 Jun 2018, 05:42 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
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 24 Jun 2018, 15:18 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

          C 1 Reply Last reply 24 Jun 2018, 18:37
          1
          • S SGaist
            24 Jun 2018, 15:18

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

            C Offline
            C Offline
            Cobra91151
            wrote on 24 Jun 2018, 18:37 last edited by
            #5

            @SGaist

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

            1 Reply Last reply
            0

            1/5

            23 Jun 2018, 09:27

            • Login

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