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 and power saving mode
Qt 6.11 is out! See what's new in the release blog

QSqlDatabase and power saving mode

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 945 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.
  • Andy314A Offline
    Andy314A Offline
    Andy314
    wrote on last edited by Andy314
    #1

    Hi,

    QSqlDatabase db;
    My progam works with one db (singleton) which should be always open. When my windows system wakes up from power saving mode, the db has lost its connection to a MSSQL-Server.

    I am looling for a solution how to manage the problem in a performant way.

    How can I get the real state of the connection ? db.isOpen() gives always true.
    Reopen with db.open() before every single DB action cost to much time.

    So If I would know the real state of the connection or got power savinig on/off events from windows I could reopen the db only in this case.

    Any suggestions ?

    C JonBJ 2 Replies Last reply
    0
    • Andy314A Andy314

      Hi,

      QSqlDatabase db;
      My progam works with one db (singleton) which should be always open. When my windows system wakes up from power saving mode, the db has lost its connection to a MSSQL-Server.

      I am looling for a solution how to manage the problem in a performant way.

      How can I get the real state of the connection ? db.isOpen() gives always true.
      Reopen with db.open() before every single DB action cost to much time.

      So If I would know the real state of the connection or got power savinig on/off events from windows I could reopen the db only in this case.

      Any suggestions ?

      C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      @Andy314 Never done this myself, so take this with a good deal of skepticism.

      Your application should receive a Windows WM_POWERBROADCAST message when it wakes. This message can be intercepted using a filter installed with QCoreApplication::installNativeEventFilter(). From there you could trigger a reconnection. You could possibly use this to make your database connection quiesce gracefully on suspend.

      Andy314A 1 Reply Last reply
      0
      • Andy314A Andy314

        Hi,

        QSqlDatabase db;
        My progam works with one db (singleton) which should be always open. When my windows system wakes up from power saving mode, the db has lost its connection to a MSSQL-Server.

        I am looling for a solution how to manage the problem in a performant way.

        How can I get the real state of the connection ? db.isOpen() gives always true.
        Reopen with db.open() before every single DB action cost to much time.

        So If I would know the real state of the connection or got power savinig on/off events from windows I could reopen the db only in this case.

        Any suggestions ?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @Andy314
        Have you at least tried any option in void QSqlDatabase::setConnectOptions(const QString &options = QString())? MYSQL_OPT_RECONNECT?

        C 1 Reply Last reply
        0
        • C ChrisW67

          @Andy314 Never done this myself, so take this with a good deal of skepticism.

          Your application should receive a Windows WM_POWERBROADCAST message when it wakes. This message can be intercepted using a filter installed with QCoreApplication::installNativeEventFilter(). From there you could trigger a reconnection. You could possibly use this to make your database connection quiesce gracefully on suspend.

          Andy314A Offline
          Andy314A Offline
          Andy314
          wrote on last edited by Andy314
          #4

          @ChrisW67 said in QSqlDatabase and power saving mode:

          @Andy314 Never done this myself, so take this with a good deal of skepticism.

          Your application should receive a Windows WM_POWERBROADCAST message when it wakes. This message can be intercepted using a filter installed with QCoreApplication::installNativeEventFilter(). From there you could trigger a reconnection. You could possibly use this to make your database connection quiesce gracefully on suspend.

          It works !
          Here is the code from Chat-GPT:

          #include <QCoreApplication>
          #include <QAbstractNativeEventFilter>
          #include <QDebug>
          #include <windows.h>
          
          class PowerEventFilter : public QAbstractNativeEventFilter
          {
          public:
              virtual bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) override
              {
                  Q_UNUSED(eventType);
                  Q_UNUSED(result);
          
                  MSG* msg = static_cast<MSG*>(message);
                  if (msg->message == WM_POWERBROADCAST) {
                      switch (msg->wParam) {
                          case PBT_APMPOWERSTATUSCHANGE:
                              qDebug() << "Power status changed.";
                              break;
                          case PBT_APMSUSPEND:
                              qDebug() << "System is suspending operation.";
                              break;
                          case PBT_APMRESUMEAUTOMATIC:
                              qDebug() << "Operation resuming automatically after event.";
                              break;
                          // ... handle other cases ...
                      }
                  }
                  return false;
              }
          };
          
          int main(int argc, char *argv[])
          {
              QCoreApplication a(argc, argv);
          
              PowerEventFilter filter;
              a.installNativeEventFilter(&filter);
          
              return a.exec();
          }
          
          
          1 Reply Last reply
          0
          • Andy314A Andy314 has marked this topic as solved on
          • JonBJ JonB

            @Andy314
            Have you at least tried any option in void QSqlDatabase::setConnectOptions(const QString &options = QString())? MYSQL_OPT_RECONNECT?

            C Offline
            C Offline
            ChrisW67
            wrote on last edited by
            #5

            @JonB Good idea for a MySQL database.

            This is Microsoft SQL Server. There does not seem to be an ODBC equivalent option on the Qt side. There may be an ODBC connection string option that achieves the same thing but I could not see one in a brief look.

            JonBJ 1 Reply Last reply
            0
            • C ChrisW67

              @JonB Good idea for a MySQL database.

              This is Microsoft SQL Server. There does not seem to be an ODBC equivalent option on the Qt side. There may be an ODBC connection string option that achieves the same thing but I could not see one in a brief look.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @ChrisW67 said in QSqlDatabase and power saving mode:

              This is Microsoft SQL Server.

              I misread the OP's original MSSQL-Server as MySQL-Server!

              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