Handling Service Broker Error Messages

Error messages fall into two categories: messages created by Service Broker and messages created by an application that uses Service Broker.

Service Broker creates an error message for a conversation when an error occurs that makes it impossible for Service Broker to continue the conversation. For example, if Service Broker cannot locate the destination service because the service does not exist in the current instance and there is no entry for the service in the routing table, Service Broker creates an error message for the conversation.

An application can end an active conversation with an error at any time. Ending a conversation with an error ends the local side of the conversation and sends an error message to the remote side of the conversation. Notice, however, that if the remote side of the conversation has already ended the conversation, Service Broker will not send the error message to the remote side of the conversation. Instead, Service Broker simply ends the conversation and removes all messages for the conversation.

The Code element of the error message contains the error code. Service Broker creates error messages that contain negative values for the error code, while error messages created by an application have a positive value for the error code. The error code that Service Broker reports is simply the negated value of the error that caused the error message. For example, when an XML validation error (error code 9615) occurs, the Database Engine creates an error message with a Code element that contains the value -9615.

In either case, once an application receives an error, the program can no longer send messages on that conversation. The application handles the error and then ends the conversation.

The exact logic a program follows to handle the error depends on the application. For example, if the program uses message retention and requires compensating transactions when the task fails, the program queries the queue for the messages that have already been processed, performs compensating transactions, and then ends the conversation. In contrast, if the program only needs to record that an error occurred, the program logs the error to a logging table and ends the conversation.

You can use application-specific error codes to communicate specific errors when you end the conversation. However, every error message ends the conversation. If your application needs to communicate errors that do not necessarily require the conversation to end, you can define a message type to represent those errors.

See Also

Other Resources

Broker Messages

Help and Information

Getting SQL Server 2005 Assistance