Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

My technology blog on SQL Server, TSQL, XML, FILESTREAM and other areas of SQL Server.
Browse by Tags · View All
XML 112
TSQL 69
XQuery 69
XQuery Functions 67
XQuery Training 65
XQuery in TSQL 64
XQuery Tutorial 63
SQL Server XQuery 63
XQuery-Labs 57
BRH 38

Archive · View All
September 2008 32
August 2008 30
July 2008 21
August 2009 19
June 2009 19
May 2010 18
January 2009 15
January 2010 14
October 2008 14
June 2008 13

Service Broker - How to clean-up all open conversations and messages?

Jul 29 2008 3:18PM by Jacob Sebastian   

These days, I am spending some time with Service Broker, testing various features exposed by SB. Many times, I am finding errors (due to my bad code) which results in dialog-conversations that are not closed, messages that are not processed etc. So, before testing something new, I needed a way to do a 'clean up' operation.

I wrote a stored procedure that iterates over all the open conversations and closes the conversation. Here is the code.

CREATE PROCEDURE CleanBrokerConversations
AS
BEGIN
BEGIN TRY
-- create a memory table. I dont prefer cursors usually
DECLARE @t TABLE( AutoID INT IDENTITY, ConversationHandle UNIQUEIDENTIFIER)

-- insert the handles of all open conversations to the
-- memory table
INSERT INTO
@t (ConversationHandle)
SELECT
[conversation_handle]
FROM
sys.conversation_endpoints

-- local variables
DECLARE @cnt INT, @max INT, @handle UNIQUEIDENTIFIER
SELECT @cnt = 1, @max = COUNT(*) FROM @t

-- run a loop for each row in the memory table
WHILE @cnt <= @max BEGIN
-- read the conversation_handle
SELECT
@handle = ConversationHandle
FROM @t WHERE AutoID = @cnt

-- end conversation
PRINT 'Closing conversation: ' + CAST(@handle AS VARCHAR(50))
END CONVERSATION @handle WITH CLEANUP

-- increment counter
SELECT @cnt = @cnt + 1
END

END TRY
BEGIN CATCH
PRINT ERROR_MESSAGE()
END CATCH
END

Closing conversations one-by-one may take very long if you have a large number of open conversations. If you have a few thousands of conversations, it may take a couple of minutes or more. There is another quick and dirty way of cleaning up all open conversations.

ALTER DATABASE BrokerTestDB SET NEW_BROKER WITH ROLLBACK IMMEDIATE;

This will clear all the open conversations much faster. However, I think this is advisable only on test environments where you need a quick way to cleanup the messages and open conversations. On production environment, it is always advisable to do close the conversations one-by-one, gracefully.

Tags: Service Broker,


Jacob Sebastian
1 · 100% · 32004
1
 
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

3  Comments  

  • You saved my life . We had thousands of poison messages in many database after a maintenance and cleaning it up one by one may take years.

    Thanks Nelson www.PracticalSqlDba.com

    commented on Apr 23 2012 5:09AM
    Nelson John A
    479 · 0% · 78
  • Good to hear that :-)

    There is an option in the voting panel above that says "This was a true life saver". You might want to use that :-)

    commented on Apr 23 2012 8:10AM
    Jacob Sebastian
    1 · 100% · 32004
  • Done :)

    commented on Apr 23 2012 8:18AM
    Nelson John A
    479 · 0% · 78

Your Comment


Sign Up or Login to post a comment.

"Service Broker - How to clean-up all open conversations and messages?" rated 5 out of 5 by 1 readers
Service Broker - How to clean-up all open conversations and messages? , 5.0 out of 5 based on 1 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]