恢复 Sql Server 数据库后启用代理

发布于 2024-09-14 03:59:04 字数 683 浏览 10 评论 0原文

我有启用了 Service Broker 的数据库。然后我想从其他数据库的备份中恢复程序中的数据库,但是恢复后(我在现有数据库名称上恢复),我的方法(启用 Service Broker)会出现此错误:

    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.

这是我的方法:

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

我应该在这里修复什么?有什么建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

自找没趣 2024-09-21 03:59:04

记下这些选项,

ALTER DATABASE mydb SET ENABLE_BROKER

ALTER DATABASE mydb SET DISABLE_BROKER

ALTER DATABASE mydb SET NEW_BROKER

如果您得到类似的信息,则已经是具有相同 ID 的已启用 Service Broker,请选择 NEW_BROKER

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

沐歌 2024-09-21 03:59:04
ALTER DATABASE [Database_name] SET NEW_BROKER WITH ROLLBACK IMMEDIATE; 

这将创建新的服务代理

ALTER DATABASE [Database_name] SET NEW_BROKER WITH ROLLBACK IMMEDIATE; 

This will create the new service broker

假面具 2024-09-21 03:59:04

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

有关详细信息,请参阅此处

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.

恋竹姑娘 2024-09-21 03:59:04

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

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
南风起 2024-09-21 03:59:04

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

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();
        }
    }

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();
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文