<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[QSqlDatabase and power saving mode]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">QSqlDatabase db;<br />
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.</p>
<p dir="auto">I am looling for a solution how to manage the problem in a performant way.</p>
<p dir="auto">How can I get the real state of the connection ?  db.isOpen() gives always true.<br />
Reopen with db.open() before every single DB action cost to much time.</p>
<p dir="auto">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.</p>
<p dir="auto">Any suggestions ?</p>
]]></description><link>https://forum.qt.io/topic/148679/qsqldatabase-and-power-saving-mode</link><generator>RSS for Node</generator><lastBuildDate>Sun, 20 Sep 2026 17:06:54 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/148679.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 29 Aug 2023 07:21:30 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QSqlDatabase and power saving mode on Wed, 30 Aug 2023 07:42:39 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/chrisw67">@<bdi>ChrisW67</bdi></a> said in <a href="/post/770768">QSqlDatabase and power saving mode</a>:</p>
<blockquote>
<p dir="auto">This is Microsoft SQL Server.</p>
</blockquote>
<p dir="auto">I misread the OP's original <code>MSSQL-Server</code> as <code>MySQL-Server</code>!</p>
]]></description><link>https://forum.qt.io/post/770812</link><guid isPermaLink="true">https://forum.qt.io/post/770812</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Wed, 30 Aug 2023 07:42:39 GMT</pubDate></item><item><title><![CDATA[Reply to QSqlDatabase and power saving mode on Wed, 30 Aug 2023 01:12:23 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> Good idea for a MySQL database.</p>
<p dir="auto">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.</p>
]]></description><link>https://forum.qt.io/post/770768</link><guid isPermaLink="true">https://forum.qt.io/post/770768</guid><dc:creator><![CDATA[ChrisW67]]></dc:creator><pubDate>Wed, 30 Aug 2023 01:12:23 GMT</pubDate></item><item><title><![CDATA[Reply to QSqlDatabase and power saving mode on Tue, 29 Aug 2023 13:14:31 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/chrisw67">@<bdi>ChrisW67</bdi></a> said in <a href="/post/770662">QSqlDatabase and power saving mode</a>:</p>
<blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/andy314">@<bdi>Andy314</bdi></a> Never done this myself, so take this with a good deal of skepticism.</p>
<p dir="auto">Your application should receive a Windows <a href="https://learn.microsoft.com/en-us/windows/win32/power/wm-powerbroadcast" target="_blank" rel="noopener noreferrer nofollow ugc">WM_POWERBROADCAST</a> message when it wakes.  This message can be intercepted using a filter installed with  <a href="https://doc.qt.io/qt-6/qcoreapplication.html#installNativeEventFilter" target="_blank" rel="noopener noreferrer nofollow ugc">QCoreApplication::installNativeEventFilter()</a>.  From there you could trigger a reconnection.  You could possibly use this to make your database connection quiesce gracefully on suspend.</p>
</blockquote>
<p dir="auto">It works !<br />
Here is the code from Chat-GPT:</p>
<pre><code>#include &lt;QCoreApplication&gt;
#include &lt;QAbstractNativeEventFilter&gt;
#include &lt;QDebug&gt;
#include &lt;windows.h&gt;

class PowerEventFilter : public QAbstractNativeEventFilter
{
public:
    virtual bool nativeEventFilter(const QByteArray &amp;eventType, void *message, long *result) override
    {
        Q_UNUSED(eventType);
        Q_UNUSED(result);

        MSG* msg = static_cast&lt;MSG*&gt;(message);
        if (msg-&gt;message == WM_POWERBROADCAST) {
            switch (msg-&gt;wParam) {
                case PBT_APMPOWERSTATUSCHANGE:
                    qDebug() &lt;&lt; "Power status changed.";
                    break;
                case PBT_APMSUSPEND:
                    qDebug() &lt;&lt; "System is suspending operation.";
                    break;
                case PBT_APMRESUMEAUTOMATIC:
                    qDebug() &lt;&lt; "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(&amp;filter);

    return a.exec();
}

</code></pre>
]]></description><link>https://forum.qt.io/post/770701</link><guid isPermaLink="true">https://forum.qt.io/post/770701</guid><dc:creator><![CDATA[Andy314]]></dc:creator><pubDate>Tue, 29 Aug 2023 13:14:31 GMT</pubDate></item><item><title><![CDATA[Reply to QSqlDatabase and power saving mode on Tue, 29 Aug 2023 08:03:44 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/andy314">@<bdi>Andy314</bdi></a><br />
Have you at least tried any option in <a href="https://doc.qt.io/qt-6/qsqldatabase.html#setConnectOptions" target="_blank" rel="noopener noreferrer nofollow ugc">void QSqlDatabase::setConnectOptions(const QString &amp;options = QString())</a>?  <code>MYSQL_OPT_RECONNECT</code>?</p>
]]></description><link>https://forum.qt.io/post/770665</link><guid isPermaLink="true">https://forum.qt.io/post/770665</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Tue, 29 Aug 2023 08:03:44 GMT</pubDate></item><item><title><![CDATA[Reply to QSqlDatabase and power saving mode on Tue, 29 Aug 2023 07:42:57 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/andy314">@<bdi>Andy314</bdi></a> Never done this myself, so take this with a good deal of skepticism.</p>
<p dir="auto">Your application should receive a Windows <a href="https://learn.microsoft.com/en-us/windows/win32/power/wm-powerbroadcast" target="_blank" rel="noopener noreferrer nofollow ugc">WM_POWERBROADCAST</a> message when it wakes.  This message can be intercepted using a filter installed with  <a href="https://doc.qt.io/qt-6/qcoreapplication.html#installNativeEventFilter" target="_blank" rel="noopener noreferrer nofollow ugc">QCoreApplication::installNativeEventFilter()</a>.  From there you could trigger a reconnection.  You could possibly use this to make your database connection quiesce gracefully on suspend.</p>
]]></description><link>https://forum.qt.io/post/770662</link><guid isPermaLink="true">https://forum.qt.io/post/770662</guid><dc:creator><![CDATA[ChrisW67]]></dc:creator><pubDate>Tue, 29 Aug 2023 07:42:57 GMT</pubDate></item></channel></rss>