SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Asynchronous SP execution via Service Broker


Asynchronous SP execution via Service Broker

Author
Message
liteswitch
liteswitch
SSC-Enthusiastic
SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)SSC-Enthusiastic (168 reputation)

Group: General Forum Members
Points: 168 Visits: 598
Hi all,

I'm looking at implementing Service Broker to resolve some performance issues in one of our applications where certain long running processes hold up the front end UI for users.

So far, I've successfully set up the steps in Service Broker for the message types, queues etc and that's working fine. What I want to do now though is in the message reply at the end, call another stored procedure which will take up to a minute or two to run, but without that impacting the return of the message reply and ending the conversation. I've added a segment of code below to show how I'm trying to do the final reply and end the conversation.


-- Receive reply and end conversation

DECLARE @ReceivedReplyMessage NVARCHAR(100);
DECLARE @ReceivedReplyHandle UNIQUEIDENTIFIER;

BEGIN TRANSACTION;

WAITFOR
( RECEIVE TOP(1)
@ReceivedReplyHandle = conversation_handle,
@ReceivedReplyMessage = message_body
FROM InitiatorCalculationsQueue
), TIMEOUT 1000;

Select
@ReceivedReplyMessage
-- T-SQL statement for running SP
EXEC SomeLongRunningSP

-- end T-SQL
END CONVERSATION @ReceivedReplyHandle;

SELECT @ReceivedReplyMessage AS ReceivedReplyMessage;

COMMIT TRANSACTION;
GO



So basically the application calls an SP which generates the initiator message and sends it off. Service Broker than accepts that, sends an acknowledgement back, calls the external stored procedure and then ends the conversation, so that the front end application can continue to work as soon as it gets the acknowledgement/reply while the database runs the Stored Procedure.

Does that make sense, or have I missed something obvious?
Nils Gustav Stråbø
Nils Gustav Stråbø
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2427 Visits: 3575
Hi

There are some issues and things you need to consider with this approach:

Since Service Broker honors transactions, the first acknowledge message will not be delivered until the transaction is committed, and since you run SomeLongRunningSP inside the same transaction as the RECEIVE statement the message will no be delivered until the sproc has completed.
I would just trust SQL Server with delivery once the initiator message has been sent (and by that I don't mean delivered at the target) and the transaction has committed. Make sure you monitor sys.transmission_queue for delivery failures. You should also set up monitoring of the status of the queues in case they are disabled (due to poison message detection). It happend a lot for me in the beginning.

What happens if execution of SomeLongRunningSP fails? You should not rollback the transaction and reread the message becuase this could lead to poison message detection and disabling of the queue. I've usually implemented retry logic by setting up a retry table together with BEGIN CONVERSATION TIMER. Monitor the retry table to detect any failures.
Database mail uses this kind of logic.

Do you need to inform the client when the sproc execution is completed?
kevin_nikolai
kevin_nikolai
Old Hand
Old Hand (367 reputation)Old Hand (367 reputation)Old Hand (367 reputation)Old Hand (367 reputation)Old Hand (367 reputation)Old Hand (367 reputation)Old Hand (367 reputation)Old Hand (367 reputation)

Group: General Forum Members
Points: 367 Visits: 523
Hi Nils, Hope you can assist.

Want to move data asap from ServerA to ServerB when user clicks on 'Submit button' in Application front-end of ServerA. Once ServerB receives the data, it gets processed by the data capturers via Application on ServerB, and thereafter some data needs to be pushed back to ServerA.
Should I use service broker for both parts (from ServerA to ServerB and vice versa) ?
Can you provide any code to create the server broker, etc. ?
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search