Results 1 to 7 of 7

Thread: Unable to commit transaction

  1. #1
    Join Date
    Jun 2008
    Location
    Rome, Italy
    Posts
    95
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Unable to commit transaction

    Hi to All,
    I'm working on a SQLITE database and it returns the following error after submiAll() in void frmUnValore_AME::submit() function when I try to run modelAME->database().commit():

    QSqlError(-1, "Unable to commit transaction", "cannot commit transaction -
    SQL statements in progress")

    it's quite strange cause I use this funtion template to edit other tables in the database (and it works). As anyone help me suggesting why it happens?
    Thanks!

    Qt Code:
    1. #include "frmUnValore_AME.h"
    2.  
    3. /*----------------------------------------------------------------------------*/
    4.  
    5. frmUnValore_AME::frmUnValore_AME(QSqlTableModel *model, const int &isItNew,
    6. QWidget *parent) : QDialog(parent)
    7. {
    8. ui.setupUi(this);
    9. this->setWindowFlags(Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
    10. this->setAttribute(Qt::WA_DeleteOnClose);
    11.  
    12. modelAME = new QSqlTableModel(this);
    13. modelAME = model;
    14.  
    15. if (modelAME->database().transaction())
    16. {
    17. qDebug() << "frmUnValore_AME TRANSACTION database!";
    18. }
    19. else
    20. {
    21. qDebug() << "frmUnValore_AME TRANSACTION FALSE!";
    22. qDebug() << modelAME->database().lastError();
    23. }
    24.  
    25.  
    26. if (isItNew < 0)
    27. {
    28. id = modelAME->rowCount();
    29. modelAME->insertRow(id);
    30. }
    31. else
    32. {
    33. id = isItNew;
    34. }
    35.  
    36. mapper = new QDataWidgetMapper(this);
    37. mapper->setModel(modelAME);
    38. mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
    39. mapper->addMapping(ui.lnePrimo, 1);
    40. mapper->setCurrentIndex(id);
    41.  
    42. connect(ui.btnAnnulla, SIGNAL(clicked()), this, SLOT(revert()));
    43. connect(ui.btnOk, SIGNAL(clicked()), this, SLOT(submit()));
    44.  
    45. qDebug() << "frmUnValore_AME Loaded!";
    46. }
    47.  
    48. /*----------------------------------------------------------------------------*/
    49.  
    50. void frmUnValore_AME::revert()
    51. {
    52. mapper->revert();
    53. qDebug() << "->frmUnValore_AME (Aggiunta Rifiutata!";
    54. modelAME->revertAll();
    55. modelAME->database().rollback();
    56.  
    57. qDebug() << "frmUnValore_AME closed!";
    58. }
    59.  
    60. /*----------------------------------------------------------------------------*/
    61.  
    62. void frmUnValore_AME::submit()
    63. {
    64. if (mapper->submit())
    65. {
    66. mapper->setCurrentIndex(id);
    67. qDebug() << "frmUnValore_AME Ok submit!";
    68.  
    69. if (modelAME->submitAll())
    70. {
    71. if (modelAME->database().commit())
    72. {
    73. qDebug() << "frmUnValore_AME COMMIT database!";
    74. }
    75. else
    76. {
    77. qDebug() << "frmUnValore_AME COMMIT FALSE!";
    78. qDebug() << modelAME->database().lastError();
    79. }
    80. }
    81. else
    82. {
    83. modelAME->revertAll();
    84. modelAME->database().rollback();
    85. QMessageBox::warning(this, tr("Attenzione!"),
    86. tr("Il database ha riportato un errore: %1")
    87. .arg(modelAME->lastError().text()));
    88. }
    89. }
    90. else
    91. {
    92. qDebug() << "No submit!";
    93. QMessageBox::warning(this, tr("Attenzione!"),
    94. tr("Il database ha riportato un errore: %1")
    95. .arg(modelAME->lastError().text()));
    96. }
    97. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jun 2008
    Location
    Rome, Italy
    Posts
    95
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Unable to commit transaction

    It has been partially solved stopping the QSqlTableModel query (modelAME) then run commit() and re-exec() the QSqlTableModel's query as follow:

    Qt Code:
    1. void frmUnValore_AME::submit()
    2. {
    3. if (mapper->submit())
    4. {
    5. mapper->setCurrentIndex(id);
    6. qDebug() << "frmUnValore_AME Ok submit!";
    7.  
    8. if (modelAME->submitAll())
    9. {
    10. modelAME->query().finish(); // Stop the query...
    11.  
    12. if (modelAME->database().commit()) //Now it works!!!
    13. {
    14. qDebug() << "frmUnValore_AME COMMIT database!";
    15. modelAME->query().exec(); //...Restart the query!
    16. }
    17. else
    18. {
    19. qDebug() << "frmUnValore_AME COMMIT FALSE!";
    20. QMessageBox::critical(this, tr("Attenzione!"),
    21. tr("Il database ha riportato un errore: \n%1")
    22. .arg(modelAME->database().lastError().text()));
    23. }
    24. }
    25. else
    26. {
    27. modelAME->revertAll();
    28. modelAME->database().rollback();
    29. QMessageBox::critical(this, tr("Attenzione!"),
    30. QString("Il database ha riportato un errore: %1")
    31. .arg(modelAME->database().lastError().text()));
    32. }
    33. }
    34. else
    35. {
    36. qDebug() << "No submit!";
    37. QMessageBox::critical(this, tr("Attenzione!"),
    38. QString("Il database ha riportato un errore: \n%1")
    39. .arg(modelAME->database().lastError().text()));
    40. }
    41. }
    To copy to clipboard, switch view to plain text mode 

    Have you ever met that behaviour?
    Would it depend by SQLITE?

    Thanks

  3. #3
    Join Date
    Mar 2008
    Posts
    141
    Thanks
    10
    Thanked 9 Times in 9 Posts

    Default Re: Unable to commit transaction

    Hi,

    maybe its due to the lazy model population you have to avoid with sqlite (QSqlQueryModel::fetchMore)

  4. #4
    Join Date
    Jun 2008
    Location
    Rome, Italy
    Posts
    95
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Unable to commit transaction

    Ok, now including that code:

    Qt Code:
    1. while (model->canFetchMore())
    2. model->fetchMore();
    To copy to clipboard, switch view to plain text mode 

    I force fetching all the records, but the error persist if I cut modelAME->query().finish(); // Stop the query...

    It's stopping me...

  5. #5
    Join Date
    Mar 2008
    Posts
    141
    Thanks
    10
    Thanked 9 Times in 9 Posts

    Default Re: Unable to commit transaction

    What is the models submit policy? I suppose OnManualSubmit is the best option for sqlite.

  6. #6
    Join Date
    Jun 2008
    Location
    Rome, Italy
    Posts
    95
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Unable to commit transaction

    Yes, it is. It would be quite strange because SQLITE drivers don't return the query size but support Transactions:

    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    2. db.setDatabaseName(db_path);
    3. qDebug() << "QSQLITE QSqlDriver::QuerySize: " << db.driver()->hasFeature(QSqlDriver::QuerySize); // FALSE
    4. qDebug() << "QSQLITE QSqlDriver::Transactions: " << db.driver()->hasFeature(QSqlDriver::Transactions); // TRUE
    To copy to clipboard, switch view to plain text mode 

    I guess it depends, maybe, on SQLITE cache size, I'm investigating...

  7. #7
    Join Date
    Jan 2008
    Posts
    39
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Unable to commit transaction

    Did you manage to solve this?

    I find things work OK until the table with the insert/update has more than 256 rows ?

Similar Threads

  1. svn commit error on XP using Qt creator
    By yycking in forum General Discussion
    Replies: 3
    Last Post: 10th April 2009, 09:47
  2. How to Compile VTKDesigner2 with Qt?
    By alfredoaal in forum Newbie
    Replies: 0
    Last Post: 5th September 2008, 06:34

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.