<?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[QSqlQuery execution resets transaction for no reason]]></title><description><![CDATA[<p dir="auto">I have a method that checks</p>
<pre><code>//transaction checker
bool common::is_in_transaction()
{
    int in_transaction = 0;
    QSqlQuery query;

    query.prepare("SELECT @@in_transaction");
    query.exec();

    if (query.next())
    {
        in_transaction = query.value(0).toInt();
        qDebug() &lt;&lt; "query in transaction: " &lt;&lt; query.value(0).toInt();
    }

    return(in_transaction);
}


//sql runner
bool common::sql_exec(QSqlQuery *query, QString query_description, bool force_debug, bool ok_to_commit)
{
    qDebug()&lt;&lt;"before sql exec in trans:"&lt;&lt;is_in_transaction();

    bool res=true;
    if (!db-&gt;isOpen())
    {
        qDebug()&lt;&lt;"Connection error @"&lt;&lt;query_description;
        return(false);
    }

    if (ok_to_commit)
        res = query-&gt;exec();
    qDebug()&lt;&lt;"after sql exec in trans:"&lt;&lt;is_in_transaction();
}

//later in my maincode I call

QSqlQuery query;
query.prepare(query_string);
common-&gt;sql_exec(&amp;query, "description",true, true);
</code></pre>
<p dir="auto">For some reason I cannot figure out after query-&gt;exec() the in_transaction gets reset to 0</p>
<p dir="auto">query in transaction:  1<br />
before sql exec in trans: true<br />
query in transaction:  0<br />
after sql exec in trans: false</p>
<p dir="auto">I never do any commits or rollbacks in my query_string, it is just a table update statement.</p>
]]></description><link>https://forum.qt.io/topic/148737/qsqlquery-execution-resets-transaction-for-no-reason</link><generator>RSS for Node</generator><lastBuildDate>Tue, 12 May 2026 00:34:30 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/148737.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 30 Aug 2023 09:19:55 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QSqlQuery execution resets transaction for no reason on Wed, 30 Aug 2023 16:43:21 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/seb-tur">@<bdi>Seb-Tur</bdi></a><br />
<code>TRUNCATE</code> commits any existing transaction.  <code>DELETE FROM TABLE</code> does not.  I would expect <code>ALTER TABLE auto_increment =1</code> to do same if it is a statement that alters a table's auto-increment column in your SQL.  Any DDL-change-type statement is likely to do so.</p>
]]></description><link>https://forum.qt.io/post/770887</link><guid isPermaLink="true">https://forum.qt.io/post/770887</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Wed, 30 Aug 2023 16:43:21 GMT</pubDate></item><item><title><![CDATA[Reply to QSqlQuery execution resets transaction for no reason on Wed, 30 Aug 2023 16:37:14 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a></p>
<p dir="auto">I wanted to be clever and replace TRUNCATE with DELETE FROM table + ALTER TABLE auto_increment =1;</p>
<p dir="auto">but alter table has the same effect -it smuggles a COMMIT under the table...<br />
So I guess clearing the indexes is a maintenance task that needs to be taken care outside of transaction</p>
]]></description><link>https://forum.qt.io/post/770886</link><guid isPermaLink="true">https://forum.qt.io/post/770886</guid><dc:creator><![CDATA[Seb Tur]]></dc:creator><pubDate>Wed, 30 Aug 2023 16:37:14 GMT</pubDate></item><item><title><![CDATA[Reply to QSqlQuery execution resets transaction for no reason on Wed, 30 Aug 2023 09:29:03 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/seb-tur">@<bdi>Seb-Tur</bdi></a><br />
This is your whole code?  The very first time you call <code>sql_exec()</code> is returns <code>before sql exec in trans: true</code>, but you have never done anything to the database?<br />
[Written before your latest post.]</p>
]]></description><link>https://forum.qt.io/post/770838</link><guid isPermaLink="true">https://forum.qt.io/post/770838</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Wed, 30 Aug 2023 09:29:03 GMT</pubDate></item><item><title><![CDATA[Reply to QSqlQuery execution resets transaction for no reason on Wed, 30 Aug 2023 09:28:14 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/seb-tur">@<bdi>Seb-Tur</bdi></a></p>
<p dir="auto">I have solved my own problem<br />
the fact is that the error happened when query_string contained TRUNCATE table statement</p>
<p dir="auto">and TRUNCATE calls commit automatically which I did not know.</p>
<p dir="auto"><a href="https://stackoverflow.com/questions/5972364/mysql-truncate-table-within-transaction" target="_blank" rel="noopener noreferrer nofollow ugc">https://stackoverflow.com/questions/5972364/mysql-truncate-table-within-transaction</a></p>
]]></description><link>https://forum.qt.io/post/770837</link><guid isPermaLink="true">https://forum.qt.io/post/770837</guid><dc:creator><![CDATA[Seb Tur]]></dc:creator><pubDate>Wed, 30 Aug 2023 09:28:14 GMT</pubDate></item></channel></rss>