If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Store procedure -- Error/rollback

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-26-09, 14:14
phil72 phil72 is offline
Registered User
 
Join Date: Nov 2008
Posts: 41
Store procedure -- Error/rollback

how can i rollback transactions in stored procedure if of the query returns error.
Reply With Quote
  #2 (permalink)  
Old 05-26-09, 14:57
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
If you don't do a commit in the SP, just issue rollback.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #3 (permalink)  
Old 05-26-09, 15:20
phil72 phil72 is offline
Registered User
 
Join Date: Nov 2008
Posts: 41
i have a procedure like this :


Insert into table1 .....

update table2 ....

Insert into table3 .....

update table4 ....

for some reason Insert into table3 ..... failed (run time error)

when i looked, data was inserted in table1 and updated in table2

i thought if procedure throws run time error, all transcations will be rolled back automatically. I dont not have commit statement in the code.
Reply With Quote
  #4 (permalink)  
Old 05-26-09, 15:54
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
No, your assumption is not correct. Why should the procedure roll back everything? If you want to have this behavior, set a savepoint at the beginning of the procedure and in case an error occurred, roll back to that savepoint.

Stored procedures are not different in any respect from transaction management. Each procedure call runs in the transactional scope defined by the caller. So whenever the caller commits or rolls back, that will determine what happens with the changes made inside the procedure. (It doesn't matter whether you have auto-commit turned on.) Furthermore, a stored procedure being basically just a container for a sequence of SQL statements can also execute a COMMIT or ROLLBACK. That has an impact on the caller's transaction. There is no autonomous transaction or so...
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #5 (permalink)  
Old 05-26-09, 17:31
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
Quote:
Originally Posted by phil72
i have a procedure like this :


Insert into table1 .....

update table2 ....

Insert into table3 .....

update table4 ....

for some reason Insert into table3 ..... failed (run time error)

when i looked, data was inserted in table1 and updated in table2

i thought if procedure throws run time error, all transcations will be rolled back automatically. I dont not have commit statement in the code.
You can put a rollback statement in a condition handler if that is what you want to happen if a negative return code is encountered. Make sure you don't rollback for warnings like +100.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On