Service Broker Poison Message HandlingMarch 23, 2009 In the recent installments of our series dedicated to topics related to SQL Server 2005 Express Edition, we have been discussing Service Broker features, focusing in particular on error handling techniques that leverage its transactional nature. As we have demonstrated, it is possible to build a certain degree of resiliency into our applications by modifying their code to address different types of obstacles that might interrupt their execution. However, it is also important to realize that there exists an internal Service Broker mechanism, known as Poison Message detection, which provides similar functionality. The purpose of this article is to describe its characteristics and present an example demonstrating its use. In general, this mechanism comes into play when a message arriving in a
queue cannot be properly processed, causing multiple rollbacks of the
transaction responsible for its retrieval. Assuming that a condition that
originally triggered the failure has not changed, each consecutive attempt to
perform the same sequence of steps will result in an identical outcome. In
order to prevent this situation from continuing in an uncontrolled manner (unnecessarily
contributing to increased utilization of system resources), five consecutive
rollbacks automatically cause the queue to become disabled (i.e. its However, regardless of your intentions, you might encounter a situation, in
which a queue processing incoming messages becomes a victim of the poison
message mechanism. If this happens, the remediation process begins typically
with turning off the ALTER QUEUE qRecv WITH ACTIVATION (STATUS = OFF) This step prevents automatic invocation of the activation stored procedure (which, after the queue got re-enabled, would likely cause another round of rollbacks, triggering poison message detection, and bringing us back to the starting point). Once the activation is turned off, you can enable the queue by executing: ALTER QUEUE qRecv WITH STATUS = ON At this point, you are ready to retrieve the message using ALTER QUEUE qRecv WITH ACTIVATION (STATUS = ON) Since the resolution described above relies on the manual intervention (and,
until it is completed, no conversations via the disabled queue are possible),
you might want to consider implementing event notification functionality,
alerting you whenever such an occurrence takes place. This capability leverages
the event type -- on srvEnt01 USE dbSBEnt01 GO CREATE QUEUE qPMNotify; GO CREATE SERVICE svcPMNotify ON QUEUE qPMNotify ([http://schemas.microsoft.com/SQL/Notifications/PostEventNotification]); GO CREATE ROUTE rtPMNotify WITH SERVICE_NAME = 'svcPMNotify', ADDRESS = 'LOCAL'; GO CREATE EVENT NOTIFICATION ePMQueueDisabled ON QUEUE qRecv FOR BROKER_QUEUE_DISABLED TO SERVICE 'svcPMNotify', 'current database' GO Obviously, we are not limited to collecting notifications using objects residing in the same database. However, if you decide to use a remote service (and its queue), you will also need to establish full dialog security for Service Broker conversations associated with it. (For more information on this subject, refer to one of our earlier articles) as well as adjust accordingly route definitions (including a route pointing back from a remote system to the local one). Once these changes are in place, you should be able to verify the resulting
functionality by monitoring the content of the The steps described above give you the ability to capture occurrences of a
queue becoming disabled due to the default poison message processing behavior;
however, you might also want to be able to implement an additional alerting
mechanism that would bring such events to your attention. One way to accomplish
this goal is to leverage the queue activation mechanism (which we have
described recently)
that relies on a stored procedure associated with the notification queue (our This article concludes our fairly extensive coverage of Service Broker related functionality available in SQL Server 2005 Express Edition. Note that the majority of topics we have presented here are applicable to the entire product line. In our next article, we will turn our attention to other database administration features. |