In earlier posts I already blogged about creating some simple queuing solutions with SQL Server service broker. last week I spend some time actually implementing my research in a customer project and of course ran into two 'Interesting' issues.
- It is possible for the service broker to somehow die on you, whilst appearing to be up and running and with retention = on sent message appear to be in the queue.
When using the SQL profiler it becomes clear that messages don't get processed because the service broker is not enabled.
You can use the following statement to enable service broker:
Alter database pfa_data set enable_broker
I had to do it on two machines and on of them gave me the following error:
MSG 9772, level 16, state 1, line 2
The Service Broker in Database "pfa_data" cannot be enabled because there is already an enabled service broker with the same ID.
MSG 5069, level 16, state 1, line 2
Alter database statement failed.
So instead I used:
Alter database pfa_data set new_broker
After this the service broker was up and running and message started getting delivered.
If the query takes extremely long to complete restart SQL Server and try again.
One potential cause for this is when you restore a database.
- In my earlier post I just blatantly used with cleanup to avoid getting enddialog messages.
The documentation however states:
With cleanup
Remove all messages and catalog view entries for this side of the conversation without policying the other side of the conversation. microsoft SQL Server drops the conversation endpoint, all messages for the conversation in the transmission queue, and all messages for the conversation in the service queue. use this option to remove conversations which cannot complete normally. for example, if the remote service has been permanently removed, you use with cleanup to remove conversations to that service.
So use this sparingly.
I 've discovered a much cleaner way to handle it.
when sending a message you can use "end conversation @ dialog" to specify that as far as the sender is concerned the dialog is over. in other words: a fire and forget message.
when the messaging side of the conversation parameter es the messages and performs its "end conversation" Statement, service broker will detect that no acknowledgement (enddialog) needs to be send.