
Conversation Group Lifetime
Service Broker manages the lifetime of the conversation group. You do not need to explicitly create or destroy a conversation group. Service Broker creates a new conversation group under the following circumstances:
-
An application begins a new conversation that is not related to an existing conversation group. Service Broker creates a new conversation group and assigns a new identifier to the conversation group.
-
An application begins a conversation related to a conversation group identifier that does not currently exist. In this case, Service Broker creates a new conversation group with the specified identifier. This means you can assign your own value to a conversation group identifier.
-
Service Broker receives the first message on a new conversation started by another service. In this case, Service Broker uses the name of the service and the broker instance identifier (if one is present) to do the following:
-
Locate the appropriate queue.
-
Create a new conversation group and associate the conversation group with the queue.
-
Create a new conversation handle and add the conversation handle to the new conversation group.
-
Place the incoming message on the queue.
Service Broker adds the conversation group identifier to the metadata for the conversation that created the conversation group. Whenever Service Broker receives a message for any conversation associated with the conversation group, Service Broker adds the conversation group identifier to that message before entering the message in the queue.
A conversation group identifier is valid from the time Service Broker creates it until all conversations associated with the identifier end; that is, the conversation group identifier is guaranteed to be valid while any conversation in the group is active.
An application that uses the conversation group identifier to manage application state uses a state table provided by the developer. The application must delete that state from the state table when the application determines that the state is no longer necessary. In many cases, the application deletes state after the task completes successfully, or after errors indicate that the task cannot be completed. In these cases, the application typically includes the command to delete state within the transaction that sends the final response message and ends the conversation. This strategy ensures that the application state and the conversation group identifier have the same lifetime. If the send operation fails, the delete operation rolls back. Likewise, if the delete operation fails, the send operation rolls back and SQL Server does not send the message. In either case, the application state and the conversation group identifier remain valid. If both operations succeed, then the conversation group identifier lifetime ends at the same time that the program deletes the associated application state.