I am interested in the side effects and potential problems of the following pattern:
CREATE PROCEDURE [Name] AS BEGIN BEGIN TRANSACTION BEGIN TRY [...Perform work, call nested procedures...] END TRY BEGIN CATCH ROLLBACK TRANSACTION RAISERROR [rethrow caught error using @ErrorNumber, @ErrorMessage, etc] END CATCH END
As far as I understand, this template sounds when used with one procedure - the procedure will either execute all its instructions without errors, or roll back all the actions and report an error.
However, when one stored procedure calls another stored procedure to do some part of the work (on the understanding that the smaller procedure is sometimes called by itself), I see a problem associated with rollbacks - an informational message (Level 16) is issued with the message The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION. . This I assume, because rollback in a subroutine always rolls back an external transaction, not just a transaction running in a subroutine.
I want everything to be thrown back and aborted if any error occurs (and the error message was reported to the client as an SQL error), I'm just not sure about all the side effects that arise from the outer layers trying to roll back the transaction, which already retractable. Perhaps checking @@TRANCOUNT before rolling back at each TRY CATCH level?
Finally, there is a client end (Linq2SQL) that has its own transaction level:
try { var context = new MyDataContext(); using (var transaction = new TransactionScope()) {
In case the stored procedure, "MySubProcedure", called inside MyStoredProcedure, causes an error, can I be sure that everything that was done earlier in MyStoredProcedure will be canceled, all Linq operations made by SubmitChanges will be discarded and finally what error will be logged? Or what I need to change in my template to ensure that the entire operation is atomic, while still allowing the use of child parts separately (i.e., Subprocedures should have the same atomic protection).
stored-procedures sql-server-2005 linq-to-sql transactions
David Jan 15 2018-10-15T00: 00
source share