Monday 13 October 2008

How to check Service Broker is Enabled or Disabled in SQL2005

Service Broker - Service Broker will be disabled when a backup is restored
You may be surprised to see your service broker application does not work after you restore a backup. The reason is that, when you restore a backup, the new Database will be restored with service broker in disabled mode. You can run the following query to check this.

SELECT is_broker_enabled FROM sys.databases WHERE database_id = DB_ID()

Run the above query in the newly restored database and it will return 0. The reason for this behavior is the following.

Each database contains a service broker identifier that uniquely identifies the service broker instance within the network. This unique identifier is used to identify the correct instance of the service broker and deliver messages. When you restore a database, the newly restored database will have the same service broker instance ID as the database from which the backup is taken. This can cause conflicts and existing service broker applications can be disturbed. To avoid this, when a database is restored, service broker is disabled.

To enable service broker you should run the following.
ALTER DATABASE dbname SET NEW_BROKER

This will reinitialize sevice broker, will assign a new service broker instance ID and clear the messages in the queues. You can run the query we saw previously to make sure that service broker is enabled. If you check the service_broker_guid column of sys.databases, you could see that a new GUID is assigned to the service broker instance of the new database.
Ref: http://www.sqlserverandxml.com/2008/08/service-broker-service-broker-will-be.html

-- Enable Service Broker:
ALTER DATABASE [Database Name] SET ENABLE_BROKER;

-- Enable Service Broker with new service_broker_guid, used when you restored any database:
ALTER DATABASE dbname SET NEW_BROKER

-- Disable Service Broker:
ALTER DATABASE [Database Name] SET DISABLE_BROKER;
Node: By default service broker is enabled for TEMPDB & MSDB databases.
For More Information: http://msdn.microsoft.com/en-us/library/ms166057.aspx

No comments: