Hello everyone ,

I have a problem and I couldn't find why? this is my program but every time commit returns false it says commit without any open transaction.
  • I use sqlite.
  • I havent any open query
  • All functions has local QSqlQuery so they killed after finishing
  • Without transaction everything works fine



Qt Code:
  1. // this is my sqlDB code
  2. // QSqlDatabase &DataManager::mainDB()
  3. //{
  4. // return db;
  5. //}
  6. //
  7.  
  8. CompanyMembersTable cmp(&sqlDB);
  9.  
  10. QString lerror;
  11. if(sqlDB.mainDB().transaction())
  12. {
  13. if(cmp.removeMembers(customer.nationalcode))
  14. {
  15. if(cst.updateEntity(customer.id(), &customer)) {
  16. for(int i=0; i<ui->lstCompMem->count(); i++) {
  17. QString nationlid = ui->lstCompMem->item(i)->data(Qt::UserRole).toString();
  18.  
  19. Customer cust;
  20. cust.nationalid = nationlid;
  21.  
  22. if(cmp.insertMember(&customer, &cust) <= 0)
  23. {
  24. lerror = cmp.lastError();
  25. break;
  26. }
  27. }
  28. if(lerror.isEmpty() && sqlDB.mainDB().commit() ) {
  29. QMessageBox::information(this, tr("Success Edit"), tr("Customer Updated successfully"));
  30. emit saved();
  31. return;
  32. }
  33. else if(lerror.isEmpty()) {
  34. lerror = sqlDB.mainDB().lastError().text();
  35. }
  36. }
  37. else lerror = cst.lastError();
  38. }
  39. else lerror = cmp.lastError();
  40. }
  41. else lerror = sqlDB.mainDB().lastError().text();
  42.  
  43. sqlDB.mainDB().rollback();
  44. QMessageBox::critical(this, tr("Edit Error"), tr("Error Happend.\n%1").arg(lerror), tr("OK"));
  45. return;
To copy to clipboard, switch view to plain text mode 

and I should mention that the below code work fine
Qt Code:
  1. CustomerTable cst(&sqlDB);
  2. CompanyMembersTable cmb(&sqlDB);
  3.  
  4. sqlDB.mainDB().transaction();
  5. if(cst.insertEntity(&customer) > 0)
  6. {
  7. foreach (Customer cust, customer.members) {
  8. if(cmb.insertMember(&customer, &cust) <= 0)
  9. {
  10. goto ercust;
  11. }
  12. }
  13. if(sqlDB.mainDB().commit()) {
  14. QMessageBox::information(this, tr("Success Insert"), tr("Customer inserted successfully"));
  15. return;
  16. }
  17. }
  18.  
  19. ercust:
  20. QMessageBox::critical(this, tr("Error Insert"), tr("There is an error in inserting customer\n%1").arg(cst.lastError()));
  21. sqlDB.mainDB().rollback();
To copy to clipboard, switch view to plain text mode 

I want to know in witch situations a transaction would be canceled automatically.
thanks in advance