[SSB] clears the queue in the service broker.

Source: Internet
Author: User
Tags sql server query

When we develop a service broker application, it may be used for testing or the client is not correctly configured. As a result, many junk queues exist in the server Queue, which is not easy for us to troubleshoot, we can use the SQL script to clear the junk data on the server:

Use Testdb
Declare   @ Conversation   Uniqueidentifier

While Exists(Select 1 FromSYS. transmission_queue)

Begin

Set @ Conversation =(Select Top 1Conversation_handleFromSYS. transmission_queue)

EndConversation@ Conversation WithCleanup

End  


You can also use transactions to speed up the process of clearing the queue.CodeAs follows:

Use testdb
Declare @ conversation uniqueidentifier
Declare @ I Int = 0 ;

While (exists (select top1
Conversation_handle
From [sys]. [transmission_queue])
Begin
While (@ I <30)
Begin

Begin tran
Set @ conversation = (select top 1
Conversation_handle
From SYS. transmission_queue
)
End conversation @ conversation with cleanup
-- Print @ I
Commit
Set @ I = @ I + 1
End
Print @ I
Set @ I = 0
End

 

 

 

If the messages received by the client are not processed, they will also be accumulated in the client queue. In fact, this is equivalent to many unread emails. We can use the following script to read the queue. After reading the message, the queue is automatically cleared:

Use Testdb
Declare   @ Recvreplymsg   Nvarchar ( 100 );
Declare   @ Recvreplydlghandle   Uniqueidentifier ;

Begin   Transaction ;
While ( 1   =   1 )
Begin
Waitfor
(Receive Top ( 1 )
@ Recvreplydlghandle   = Conversation_handle,
@ Recvreplymsg   = Message_body
From DBO. test_targetqueue
), Timeout 1000 ;
If (@ rowcount = 0)
Begin
Rollback transaction;
Break;
End

EndConversation@ Recvreplydlghandle;

Select@ Recvreplymsg AsReceivedreplymsg;
End 

Commit Transaction;  


In the transmission queue, we can use the following statement to splice the end handle session:

 

/* ***** Script for selecttopnrows command from SSMs ***** */
Select top 1000 ' End conversation ''' + Cast (conversation_handle as nvarchar ( 50 ) + ''' With cleanup ' , Casted_message_body =
Case message_type_name when ' X '
Then cast (message_body as nvarchar (max ))
Else message_body
End
From [testdb]. [sys]. [transmission_queue]

Copy the first column and run it in the SQL Server query window.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.