Question

I have DataBase with enabled Service Broker. Then I want to restore my database in program from backup of other database, but after restoring(I restore on existing database name), my method, whitch enables Service Broker, puts this error:

    Msg 9772, Level 16, State 1, Line 1
The Service Broker in database "ServeDB2" cannot be enabled because there is already an enabled Service Broker with the same ID.
Msg 5069, Level 16, State 1, Line 1
ALTER DATABASE statement failed.

This is my method:

public void TurnOnBroker()
{
    if (!this.database.BrokerEnabled)
    {
        this.server.KillAllProcesses(this.database.Name);
        this.database.BrokerEnabled = true;
        this.database.Alter();
        RefreshConnection();
    }
}

What should i fix here?Any suggestions?

Was it helpful?

Solution 4

I found a very simple solution for that- just simlpy assign new service broker, like this:

public void TurnOnBroker()
    {
        if (!this.database.BrokerEnabled)
        {
            this.server.KillAllProcesses(this.database.Name);

            string brokerCommand = String.Format("ALTER DATABASE {0} SET NEW_BROKER", this.database.Name);
            this.database.ExecuteNonQuery(brokerCommand);

            RefreshConnection();
        }
    }

OTHER TIPS

keep a note of these options

ALTER DATABASE mydb SET ENABLE_BROKER

ALTER DATABASE mydb SET DISABLE_BROKER

ALTER DATABASE mydb SET NEW_BROKER

if youre getting something like this is already an enabled Service Broker with the same ID, go for the NEW_BROKER

ALTER DATABASE [Database_name] SET NEW_BROKER WITH ROLLBACK IMMEDIATE; 

This will create the new service broker

Every database has a unique ID used by Service Broker. This ID must be unique across all databases in a Sql Server instance (well, it should be unique globally, but Sql Server doesn't have a way to enforce that). When you restore a database, you have an option of disabling Service Broker in the restored database, enabling it with the GUID of the backed up database (so that it can take over message processing from the backed up database) or assign it a new GUID. You're trying to do the second option while you still have the old database around and you run into GUID conflict.

See here for more info.

Run this query to find out which other databases are using the same service broker as the database you are using (e.g. for a database called DATABASE_NAME)...

SELECT name, is_broker_enabled, service_broker_guid FROM sys.databases 
WHERE service_broker_guid IN (SELECT service_broker_guid FROM sys.databases where name = 'DATABASE_NAME');

... returns ...

name, is_broker_enabled, service_broker_guid
DATABASE_NAME_OTHER, 1, KBHDBVJH-SDVHIOHD-SODIVDIOH-UHDSV
DATABASE_NAME_ANOTHER, 0, KBHDBVJH-SDVHIOHD-SODIVDIOH-UHDSV
DATABASE_NAME, 0, KBHDBVJH-SDVHIOHD-SODIVDIOH-UHDSV

Then run the following queries to obtain a new broker for your database...

ALTER DATABASE DATABASE_NAME SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
ALTER DATABASE DATABASE_NAME SET NEW_BROKER;
ALTER DATABASE DATABASE_NAME SET MULTI_USER;

Run the first query again and your database should be the only one in the list...

SELECT name, is_broker_enabled, service_broker_guid FROM sys.databases 
WHERE service_broker_guid IN (SELECT service_broker_guid FROM sys.databases where name = 'DATABASE_NAME');

... now returns ...

name, is_broker_enabled, service_broker_guid
DATABASE_NAME, 1, ASJCBUHBC-7UIOSUI-IUGGUI87-IUGHUIG
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top