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. QSqlQuery execution resets transaction for no reason
Qt 6.11 is out! See what's new in the release blog

QSqlQuery execution resets transaction for no reason

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 624 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.
  • Seb TurS Offline
    Seb TurS Offline
    Seb Tur
    wrote on last edited by Seb Tur
    #1

    I have a method that checks

    //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() << "query in transaction: " << 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()<<"before sql exec in trans:"<<is_in_transaction();
    
        bool res=true;
        if (!db->isOpen())
        {
            qDebug()<<"Connection error @"<<query_description;
            return(false);
        }
    
        if (ok_to_commit)
            res = query->exec();
        qDebug()<<"after sql exec in trans:"<<is_in_transaction();
    }
    
    //later in my maincode I call
    
    QSqlQuery query;
    query.prepare(query_string);
    common->sql_exec(&query, "description",true, true);
    

    For some reason I cannot figure out after query->exec() the in_transaction gets reset to 0

    query in transaction: 1
    before sql exec in trans: true
    query in transaction: 0
    after sql exec in trans: false

    I never do any commits or rollbacks in my query_string, it is just a table update statement.

    Seb TurS JonBJ 2 Replies Last reply
    0
    • Seb TurS Seb Tur

      I have a method that checks

      //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() << "query in transaction: " << 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()<<"before sql exec in trans:"<<is_in_transaction();
      
          bool res=true;
          if (!db->isOpen())
          {
              qDebug()<<"Connection error @"<<query_description;
              return(false);
          }
      
          if (ok_to_commit)
              res = query->exec();
          qDebug()<<"after sql exec in trans:"<<is_in_transaction();
      }
      
      //later in my maincode I call
      
      QSqlQuery query;
      query.prepare(query_string);
      common->sql_exec(&query, "description",true, true);
      

      For some reason I cannot figure out after query->exec() the in_transaction gets reset to 0

      query in transaction: 1
      before sql exec in trans: true
      query in transaction: 0
      after sql exec in trans: false

      I never do any commits or rollbacks in my query_string, it is just a table update statement.

      Seb TurS Offline
      Seb TurS Offline
      Seb Tur
      wrote on last edited by
      #2

      @Seb-Tur

      I have solved my own problem
      the fact is that the error happened when query_string contained TRUNCATE table statement

      and TRUNCATE calls commit automatically which I did not know.

      https://stackoverflow.com/questions/5972364/mysql-truncate-table-within-transaction

      1 Reply Last reply
      0
      • Seb TurS Seb Tur has marked this topic as solved on
      • Seb TurS Seb Tur

        I have a method that checks

        //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() << "query in transaction: " << 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()<<"before sql exec in trans:"<<is_in_transaction();
        
            bool res=true;
            if (!db->isOpen())
            {
                qDebug()<<"Connection error @"<<query_description;
                return(false);
            }
        
            if (ok_to_commit)
                res = query->exec();
            qDebug()<<"after sql exec in trans:"<<is_in_transaction();
        }
        
        //later in my maincode I call
        
        QSqlQuery query;
        query.prepare(query_string);
        common->sql_exec(&query, "description",true, true);
        

        For some reason I cannot figure out after query->exec() the in_transaction gets reset to 0

        query in transaction: 1
        before sql exec in trans: true
        query in transaction: 0
        after sql exec in trans: false

        I never do any commits or rollbacks in my query_string, it is just a table update statement.

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

        @Seb-Tur
        This is your whole code? The very first time you call sql_exec() is returns before sql exec in trans: true, but you have never done anything to the database?
        [Written before your latest post.]

        Seb TurS 1 Reply Last reply
        0
        • JonBJ JonB

          @Seb-Tur
          This is your whole code? The very first time you call sql_exec() is returns before sql exec in trans: true, but you have never done anything to the database?
          [Written before your latest post.]

          Seb TurS Offline
          Seb TurS Offline
          Seb Tur
          wrote on last edited by
          #4

          @JonB

          I wanted to be clever and replace TRUNCATE with DELETE FROM table + ALTER TABLE auto_increment =1;

          but alter table has the same effect -it smuggles a COMMIT under the table...
          So I guess clearing the indexes is a maintenance task that needs to be taken care outside of transaction

          JonBJ 1 Reply Last reply
          0
          • Seb TurS Seb Tur

            @JonB

            I wanted to be clever and replace TRUNCATE with DELETE FROM table + ALTER TABLE auto_increment =1;

            but alter table has the same effect -it smuggles a COMMIT under the table...
            So I guess clearing the indexes is a maintenance task that needs to be taken care outside of transaction

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

            @Seb-Tur
            TRUNCATE commits any existing transaction. DELETE FROM TABLE does not. I would expect ALTER TABLE auto_increment =1 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.

            1 Reply Last reply
            1

            • Login

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