Quantcast
Channel: SQL Service Broker forum
Viewing all articles
Browse latest Browse all 461

Transaction Count Oddity Within Activated Sprocs

$
0
0

Hello.

As I have been going through various scenarios in preparation of my code, I noticed that if I send a message to a queue with activation, the transaction count inside the sproc, before any code is run, is 2.

It is also 2  after I issue the BEGIN TRANSACTION; to RECEIVE the message(s), and 2 after I COMMIT before existing.  Same thing with XACT_STATE().

Now, the RECEIVE-ing is indeed affected by internal transactions, and a brute ROLLBACK does keep the message in the queue as well as disabling it.  The question is about the coding patterns everywhere - it appears that "if (@@trancount > 0) rollback transaction;" is useless (apart from ensuring the ROLLBACK is not an orphan) since one cannot count on the @@trancount as it is always >1, and would only work if there is NO code in the ELSE section of that "if (@@trancount > 0)".

I tested this by copying the code from the MS Lessons as is: SSSB Lessons(3), and just changed the activated sproc to create a trace:

CREATE or alter PROCEDURE TargetActivProc

...
  insert into dbo.debug_message(msg) values ('trancount='+cast(@@trancount as varchar)+'');


  WHILE (1=1)
  BEGIN
    BEGIN TRANSACTION;
    WAITFOR
    ( RECEIVE TOP(1)

....

    COMMIT TRANSACTION;
    --rollback TRANSACTION;
    insert into dbo.debug_message(msg) values
        ('trancount='+cast(@@trancount as varchar)+''),
      ('after commit...');
  END -- end while()
go

Although everything seems to be working in toy scenarios and I will be proceeding with my designs using SB, I am very uncomfortable with the situation since obviously, I am not understanding something.

Thanks for any help!!!

Rafael




Viewing all articles
Browse latest Browse all 461

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>