Nested stored procedures containing TRY CATCH ROLLBACK pattern?

后端 未结 5 2127

I\'m interested in the side effects and potential problems of the following pattern:

CREATE PROCEDURE [Name]
AS
BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
            


        
5条回答
  •  萌比男神i
    2020-11-22 11:14

    In case no special error handling needed in CATCH except rethrow and stored procs call chain isn't too long it may be suitable to use such simple template:

    create procedure someNestedSP
    as
    SET XACT_ABORT ON
    begin transaction
    -- do some work or call some other similar SP
    commit transaction
    

    It would also rollback root transaction with all "nested" ones in case of any error but the code is shorter and more straightforward than @gbn's solution. Still XACT_ABORT takes care of most issues mentioned there.

    There may be addiotional overhead for transaction nesting but it may be not too high, I guess.

提交回复
热议问题