36

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?

4

5 回答 5

67

记下这些选项

ALTER DATABASE mydb SET ENABLE_BROKER

ALTER DATABASE mydb SET DISABLE_BROKER

ALTER DATABASE mydb SET NEW_BROKER

如果你得到这样的东西已经是具有相同 ID 的已启用服务代理,请选择 NEW_BROKER

于 2011-06-22T12:15:42.150 回答
38
ALTER DATABASE [Database_name] SET NEW_BROKER WITH ROLLBACK IMMEDIATE; 

这将创建新的服务代理

于 2014-09-15T04:29:53.460 回答
10

每个数据库都有一个 Service Broker 使用的唯一 ID。此 ID 在 Sql Server 实例中的所有数据库中必须是唯一的(好吧,它应该是全局唯一的,但 Sql Server 没有办法强制执行此操作)。还原数据库时,您可以选择禁用已还原数据库中的 Service Broker,使用备份数据库的 GUID 启用它(以便它可以从备份数据库接管消息处理)或为其分配新的 GUID . 当您仍然拥有旧数据库并且遇到 GUID 冲突时,您正在尝试执行第二个选项。

请参阅此处了解更多信息。

于 2010-08-16T17:25:06.053 回答
5

运行此查询以找出哪些其他数据库正在使用与您正在使用的数据库相同的服务代理(例如,对于名为 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');

...返回...

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

然后运行以下查询为您的数据库获取新代理...

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

再次运行第一个查询,您的数据库应该是列表中唯一的一个...

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');

...现在返回...

name, is_broker_enabled, service_broker_guid
DATABASE_NAME, 1, ASJCBUHBC-7UIOSUI-IUGGUI87-IUGHUIG
于 2017-06-06T13:51:17.443 回答
4

我为此找到了一个非常简单的解决方案——只需简单地分配新的服务代理,如下所示:

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();
        }
    }
于 2010-08-17T09:07:51.567 回答