Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. SQL transaction rollback problem

SQL transaction rollback problem

Scheduled Pinned Locked Moved C / C++ / MFC
c++databasehelpsql-serversysadmin
4 Posts 2 Posters 0 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.
  • M Offline
    M Offline
    Moonis Ahmed
    wrote on last edited by
    #1

    hi,i am new to visual C++ programming. i have a simple visual C++ program that involves basic sql transactions and if a query fails then i have tried to rollback all the previous transactions as well. I am using MS SQL Server. the problem is that the rollback is not working in the tables in the server while when i run the program in the debug mode it shows that the rollback feature is working as it returns a positive integer from the rollback() function. the program shows no errors or warnings. Can anyone provide some suggestions? For testing i have used three queries in which the third one has been intentionally left wrong. I need the first two queries to be rollbacked when the error occurs in the third query. Would be glad if you can point out other errors as i am still learning C++ programming... Attached is the code: class test { public: void testmethod() { CDatabase dbobject[100]; static int counter=0; char query [3][100]={{"Insert into product values (1,'keyboard')"},{"Insert into product values (2,'mouse')"},{"Insert into product values (2,'cable')"}}; try { for(int i=0;i<(sizeof(dbobject)/sizeof(dbobject[0]));i++) { dbobjectIdea.OpenEx(_T( "DSN=mytest" ),CDatabase::noOdbcDialog ); dbobjectIdea.BeginTrans(); dbobjectIdea.ExecuteSQL(queryIdea); dbobjectIdea.CommitTrans(); cout<<"Query Execution Successful\n"; counter++; dbobjectIdea.Close(); } } catch (CDBException *pEx) { pEx->ReportError(); dbobject[counter].Close(); for(int j=counter-1;j>=0;j--) { dbobject[j].OpenEx(_T( "DSN=mytest" ),CDatabase::noOdbcDialog ); dbobject[j].BeginTrans(); int c = dbobject[j].Rollback(); dbobject[j].Close(); cout<<"SQL Query Rollbacked!\n"; } } } }; void main() { test t; t.testmethod(); }

    Z 1 Reply Last reply
    0
    • M Moonis Ahmed

      hi,i am new to visual C++ programming. i have a simple visual C++ program that involves basic sql transactions and if a query fails then i have tried to rollback all the previous transactions as well. I am using MS SQL Server. the problem is that the rollback is not working in the tables in the server while when i run the program in the debug mode it shows that the rollback feature is working as it returns a positive integer from the rollback() function. the program shows no errors or warnings. Can anyone provide some suggestions? For testing i have used three queries in which the third one has been intentionally left wrong. I need the first two queries to be rollbacked when the error occurs in the third query. Would be glad if you can point out other errors as i am still learning C++ programming... Attached is the code: class test { public: void testmethod() { CDatabase dbobject[100]; static int counter=0; char query [3][100]={{"Insert into product values (1,'keyboard')"},{"Insert into product values (2,'mouse')"},{"Insert into product values (2,'cable')"}}; try { for(int i=0;i<(sizeof(dbobject)/sizeof(dbobject[0]));i++) { dbobjectIdea.OpenEx(_T( "DSN=mytest" ),CDatabase::noOdbcDialog ); dbobjectIdea.BeginTrans(); dbobjectIdea.ExecuteSQL(queryIdea); dbobjectIdea.CommitTrans(); cout<<"Query Execution Successful\n"; counter++; dbobjectIdea.Close(); } } catch (CDBException *pEx) { pEx->ReportError(); dbobject[counter].Close(); for(int j=counter-1;j>=0;j--) { dbobject[j].OpenEx(_T( "DSN=mytest" ),CDatabase::noOdbcDialog ); dbobject[j].BeginTrans(); int c = dbobject[j].Rollback(); dbobject[j].Close(); cout<<"SQL Query Rollbacked!\n"; } } } }; void main() { test t; t.testmethod(); }

      Z Offline
      Z Offline
      zhang800605
      wrote on last edited by
      #2

      Sorry for my poor English . I can't tell you the reason of error . It is different in debug and release , so you can debug in release state . Project settings -> link -> Generate debug info (selected) Project settings -> C/C++ -> Debug info(select program database) Project settings -> C/C++ -> Optimizations(select disable(debug)) Then pass F5 to run program used for debug . 我不能够告诉你错误的原因。debug 版本和 release 版本是不同的,你可以在release状态下调试。你做如下设置 Project settings -> link -> Generate debug info (selected) Project settings -> C/C++ -> Debug info(select program database) Project settings -> C/C++ -> Optimizations(select disable(debug)) 然后就可以f5进行运行调试了。

      M 2 Replies Last reply
      0
      • Z zhang800605

        Sorry for my poor English . I can't tell you the reason of error . It is different in debug and release , so you can debug in release state . Project settings -> link -> Generate debug info (selected) Project settings -> C/C++ -> Debug info(select program database) Project settings -> C/C++ -> Optimizations(select disable(debug)) Then pass F5 to run program used for debug . 我不能够告诉你错误的原因。debug 版本和 release 版本是不同的,你可以在release状态下调试。你做如下设置 Project settings -> link -> Generate debug info (selected) Project settings -> C/C++ -> Debug info(select program database) Project settings -> C/C++ -> Optimizations(select disable(debug)) 然后就可以f5进行运行调试了。

        M Offline
        M Offline
        Moonis Ahmed
        wrote on last edited by
        #3

        hi, i used the settings you told me. The problem is still there. The rollback does not remove the previous two entries. This time it also shows some 'Detected Memory Leaks!' error. What does that mean?

        1 Reply Last reply
        0
        • Z zhang800605

          Sorry for my poor English . I can't tell you the reason of error . It is different in debug and release , so you can debug in release state . Project settings -> link -> Generate debug info (selected) Project settings -> C/C++ -> Debug info(select program database) Project settings -> C/C++ -> Optimizations(select disable(debug)) Then pass F5 to run program used for debug . 我不能够告诉你错误的原因。debug 版本和 release 版本是不同的,你可以在release状态下调试。你做如下设置 Project settings -> link -> Generate debug info (selected) Project settings -> C/C++ -> Debug info(select program database) Project settings -> C/C++ -> Optimizations(select disable(debug)) 然后就可以f5进行运行调试了。

          M Offline
          M Offline
          Moonis Ahmed
          wrote on last edited by
          #4

          hey, thanks for your advice. i solved the problem myself. as you see in my code, i was using an array. i didnt need to use that. i used only a single object and treated the three transactions as a single transaction. it works now.!! - Moonis

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups