Scenario
As noted in various Service Broker sources, it is often advantageous to minimize the overhead of creating dialogs to send messages on. This sample shows how to create a shared pool of dialogs to be able to reuse dialogs instead of creating new ones.
The code shows how to get, free and delete dialogs from a dialog pool table. Initially empty, a new dialog is created in the pool when a request for an existing free dialog cannot be met. Thus the pool will grow during bursts of high demand.
The dialog pool entries also contain creation time and send count fields that ease the auditing and "recycling" of dialogs in the pool based on application requirements. Recycling consists of gracefully ending an existing dialog between services and beginning a new one. If done prudently, this technique can ease the handling of dialog errors by limiting the number of messages affected. For example, the application may choose to contrain a dialog to a certain number of messages before it is recycled. This might also be done according to the age of a dialog. See the end of the usp_send procedure for an example of recycling.
An example application that exercises the dialog pool is also included.
Running the Sample
-
Open the project file in SQL Server Management Studio.
-
Run the scripts, in order:
-
1 - Demo database setup.sql
-
2 - Dialog pool setup.sql
-
3 - Demo app setup.sql
-
4 - Run demo.sql
-
5 - Cleanup.sql
-
1 - Demo database setup.sql