Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TransactionScope and Stored Procedure?

I have two PL/SQL Stored procedure each handling its own Transaction (Begin/Commit and Rollback in case of error). From .Net code I Call these two SP as shown below.

   using (TransactionScope ts = new TransactionScope())
     {
          CallSP1();
          CallSP2().
          ts.SetComplete();
     }

If my Call to SP2 fails will it roll back the changes made by CallSP1()? If it does not roll back then does that mean that its better to handle the Transaction from the .Net application instead of inside Stored Procedure?

like image 290
Amitabh Avatar asked Dec 09 '22 17:12

Amitabh


2 Answers

If the first stored procedure SP1 issues a commit then any changes it has already made will be permanent. In that case if SP2 fails, the changes made by SP1 won't be rolled back.

IMO the calling application should be the one handling the transaction logic, i.e. don't issue commit or rollback in your PL/SQL procedures. Let the errors propagate to the calling application, that way the PL/SQL engine will roll back only the work done by the failing procedure and not the entire transaction.

Let the calling application decide what to do in case of error (retry, commit half-work? or rollback).

like image 164
Vincent Malgrat Avatar answered Dec 31 '22 16:12

Vincent Malgrat


If SP1 performs a commit, it doesn't matter what happens in SP2. The changes made in SP1 will not be rolled back - they've been committed.

like image 43
DCookie Avatar answered Dec 31 '22 16:12

DCookie