Results 1 to 9 of 9

Thread: rollback issue with database

  1. #1
    Join Date
    Nov 2006
    Posts
    72
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default rollback issue with database

    I am developing Qt application which connects to MySQL database. I want to use rollback() method when insertion of some table fails (for example, if insertion of new record in table goes ok, but insertion of record in relation table fails for some reason, I would like to rollback insertion of succesfully inserted record in table). Now, this is my code :

    Qt Code:
    1. if ((transaction=m_dbm->DBM_setTransaction(this))==true)
    2. {
    3. if (m_dbm->DBM_updateTestCase(testCase,this))
    4. //in update TestCase is several insertions and updates called. I execute in this function several queries
    5. {
    6. if (m_dbm->DBM_commit(this))
    7. QMessageBox::information(this,"Message", QString("TestCase table has been successfully updated"),
    8. QMessageBox::Ok, QMessageBox::NoButton );
    9. }
    10. else
    11. {
    12. m_dbm->DBM_callRollback(this);
    13. QMessageBox::information(this,"Message", QString("Update of TestCase table has failed"),
    14. QMessageBox::Ok, QMessageBox::NoButton );
    15. }
    16. }
    17. else
    18. {
    19. if (m_dbm->DBM_updateTestCase(testCase,this))
    20. QMessageBox::information(this,"Message", QString("TestCase table has been successfully updated"),
    21. QMessageBox::Ok, QMessageBox::NoButton );
    22. else
    23. QMessageBox::information(this,"Message", QString("Update of TestCase table has failed"),
    24. QMessageBox::Ok, QMessageBox::NoButton );
    25. }
    To copy to clipboard, switch view to plain text mode 
    Now, when my insertion fails (which i know by qsqlquery messages) shown code doesn't rolls back data inserted in function DBM_updateTestCase. I don't know what am I doing wrong. Thanx for any reply.
    Last edited by jacek; 16th February 2007 at 11:40. Reason: wrapped too long lines

  2. #2
    Join Date
    Nov 2006
    Posts
    72
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: rollback issue with database

    Can anybody answer this at least. Does rollback undoes just last query or all queries in between calling() transaction and calling rollback()?

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: rollback issue with database

    Which MySQL database are we talking about? Does it support transactions? Use QSqlDriver::hasFeature to test for QSqlDriver::Transactions.

    Just to answer the last question - rollback cancels all queries since the beginning of the transaction.

  4. #4
    Join Date
    Nov 2006
    Posts
    72
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: rollback issue with database

    Well, the code of my function DBM_callTransaction() is this:

    Qt Code:
    1. bool DbmApi::DBM_setTransaction(QWidget * parent)
    2. {
    3. return m_dbLocalDb.transaction();
    4. }
    To copy to clipboard, switch view to plain text mode 
    And when you look at the code above you will see that I check does database supports transaction. I've checked, and DBM_callTransaction() returns true .That is why this behavior is strange, at least to me.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: rollback issue with database

    What happens if you execute the same SQL code from the MySQL console?

  6. #6
    Join Date
    Nov 2006
    Posts
    72
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: rollback issue with database

    Happens the same thing as when I run the application. It fails on some queries because some values (which by declaration can't be NULL) are empty. That's why queries fail. So, I would like to have option, in cases of this kind, to roll back queries that have been succesfull. See example I have described in my first post on this topic why I need that. Thank you for your replies.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: rollback issue with database

    I meant you to check whether rollback rolls back the transaction correctly when using the console.
    sql Code:
    1. BEGIN TRANSACTION;
    2. INSERT INTO ...;
    3. ROLLBACK;
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    May 2006
    Location
    Germany
    Posts
    108
    Thanks
    2
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: rollback issue with database

    From what I understood, you are either looking for savepoints in transactions (which mysql doesn't support, afaik), or you want to validate, and possibly correct, your data before starting the transaction.
    "If you lie to the compiler, it will get its revenge." - Henry Spencer

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: rollback issue with database

    Maybe it'd work if you used QSqlDriver::beginTransaction() instead of QSqlDatabase::transaction()?

Similar Threads

  1. Threads and database connection
    By probine in forum Qt Programming
    Replies: 9
    Last Post: 7th August 2013, 08:30
  2. updating database
    By locus in forum Qt Programming
    Replies: 3
    Last Post: 27th January 2007, 05:54
  3. Database access issue
    By Gayathri in forum Newbie
    Replies: 3
    Last Post: 23rd November 2006, 07:41
  4. Issues regarding QMySql drivers and mysql database
    By bera82 in forum Qt Programming
    Replies: 2
    Last Post: 10th August 2006, 17:50
  5. Filling combobox from database
    By Philip_Anselmo in forum Qt Programming
    Replies: 3
    Last Post: 11th May 2006, 17:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.