Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Error Handling in Transactions
Message
From
28/08/2002 17:12:54
 
 
To
28/08/2002 16:05:31
General information
Forum:
Microsoft SQL Server
Category:
Other
Miscellaneous
Thread ID:
00694172
Message ID:
00694680
Views:
19
>So if I understand you correctly, you would duplicate the "worker" SP and remove the trans from within it and then this "caller" SP would completely control the trans?

Not exactly. You could just leave transaction management to the first procedure that starts a transaction.
--------------------------
-- WORKER SP (inner SP being called)
--------------------------
DECLARE @useLocalTran int
IF @@TRANCOUNT = 0
 SET @useLocalTran = 1
ELSE
 SET @useLocalTran = 0

IF @useLocalTran = 1 BEGIN TRANSACTION

INSERT INTO dbo.trans (account_id, description, amount, ccnumber, type, pproved) VALUES (@intAccountID, @strDescription, @strAmount, @strCCNumber, strType, @strApproved)
 SET @intError = @@ERROR
 BEGIN
   IF @intError != 0
    BEGIN
      IF @useLocalTran = 1 AND @@TRANCOUNT > 0 ROLLBACK TRANSACTION
      EXEC dbo._Errors_Insert @@ERROR, '_Trans_Insert'
    END
   ELSE
    BEGIN
     IF @useLocalTran = 1 AND @@TRANCOUNT > 0 COMMIT TRANSACTION
    END
 END

RETURN CASE WHEN @intError = 0 THEN 0 ELSE -1 END
Notice that I've changed your proc to only manage transactions if no transaction has been started.

This is just one possibility. There's nothing wrong with each proc managing its own transactions as long as you remember that the first rollback will rollback all of the transactions.

There is no rule or best practice unless you're running under COM+. If you are, it's my preference to let COM+ manage the transaction.

Also, you don't really need to use SET XACT_ABORT.

-Mike
Michael Levy
MCSD, MCDBA
ma_levy@hotmail.com
Previous
Reply
Map
View

Click here to load this message in the networking platform