Transactionscope with multiple Storedprocedures

Asked

Viewed 93 times

1

I’m thinking of implementing TransactionScope in my code, however, all bank access is made by StoredProcedures.

The TransactionScope can give rollback in multiple actions carried out in StoredProcedures?

using (var trans = new TransactionScope()) {
  try {
    this.Teste1Repository.Insert(); //  Chamada a primeira PROC
    this.Teste2Repository.Insert(); //  Chamada a segunda PROC

    trans.Complete();
  }
  catch {
  //  Trata erro
  }
}

1 answer

2


If the first Storedprocedure SP1 issues a commit, the changes that have already been made will be permanent. In this case, if SP2 fails, the changes made by SP1 will not be reversed.

Either you control your system operations via Code, or via Database.

If you want to control the Rollbacks via code, you will have to rewrite the entire Procedure function to C#.

Source: Our own experience.

EDIT 1: Test this concept using Sqlcommand of the C#. https://stackoverflow.com/a/18718722/6579651

  • NOTE: If you want to continue using the Procedures, you will have to implement Rollback on your own. Here is an example in SQL: https://stackoverflow.com/a/13171451/6579651

  • In this case, procs do not have transactions. Therefore, the Transactionscope rollback would work, correct?

  • It wouldn’t work, because what matters in this case is Commit, since SP1 has already executed its entire function, gave a commit, went back to C# and ran SP2. In this case you would have to join the 2 Procedures in 1 only, and do the Rollback treatment within the same.

  • Even without the code starting a transaction and committing? Because my procs do not have transaction/commit/rollback.

  • @Claudioneto I don’t understand what your difficulty is. I answered your question just above, what is going wrong there?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.