QSqlDatabase and power saving mode
-
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 ?
-
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 ?
@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.
-
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 ?
-
@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.
@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(); } -
A Andy314 has marked this topic as solved on
-
@Andy314
Have you at least tried any option in void QSqlDatabase::setConnectOptions(const QString &options = QString())?MYSQL_OPT_RECONNECT? -
@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.