在 SQL Server 2008 中启用 Service Broker

发布于 2024-08-31 00:09:47 字数 920 浏览 5 评论 0原文

我正在集成 SqlCacheDependency 以在我的 LinqToSQL 数据上下文中使用。

我正在使用此处找到的 Linq 查询的扩展类 - http://code.msdn.microsoft.com/linqtosqlcache

我已经连接了代码,当我打开页面时,我收到此异常 -

“当前数据库的 SQL Server Service Broker 未启用,因此不支持查询通知。请启用 Service Broker如果您想使用通知,请访问此数据库。”

它来自 global.asax 中的此事件,

        protected void Application_Start()
    {
        RegisterRoutes(RouteTable.Routes);
        //In Application Start Event
        System.Data.SqlClient.SqlDependency.Start(new dataContextDataContext().Connection.ConnectionString);

    }

我的问题是...

  1. 如何在 SQL Server 2008 数据库中启用 Service Broker?我尝试运行此查询.. ALTER DATABASE tablename SET ENABLE_BROKER 但它永远不会结束并永远运行,我必须手动停止它。

  2. 一旦我在 SQL Server 2008 中设置了此设置,它是否会过滤到我的 DataContext,或者我是否还需要在那里配置某些内容?

的帮助

感谢您

I am integrating SqlCacheDependency to use in my LinqToSQL datacontext.

I am using an extension class for Linq querys found here - http://code.msdn.microsoft.com/linqtosqlcache

I have wired up the code and when I open the page I get this exception -

"The SQL Server Service Broker for the current database is not enabled, and as a result query notifications are not supported. Please enable the Service Broker for this database if you wish to use notifications."

its coming from this event in the global.asax

        protected void Application_Start()
    {
        RegisterRoutes(RouteTable.Routes);
        //In Application Start Event
        System.Data.SqlClient.SqlDependency.Start(new dataContextDataContext().Connection.ConnectionString);

    }

my question is...

  1. how do i enable Service Broker in my SQL server 2008 database? I have tried to run this query.. ALTER DATABASE tablename SET ENABLE_BROKER but it never ends and runs for ever, I have to manually stop it.

  2. once I have this set in SQL server 2008, will it filter down to my DataContext, or do I need to configure something there too ?

thanks for any help

Truegilly

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

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

发布评论

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

评论(7

稳稳的幸福 2024-09-07 00:09:47

如果其他人正在寻找此问题的解决方案,以下命令对我来说非常有用。它释放与数据库的所有其他连接,而不是等待。

ALTER DATABASE [DBNAME] SET ENABLE_BROKER WITH ROLLBACK IMMEDIATE

In case anyone else is looking for a solution to this problem, the following command worked great for me. It releases all other connections to the database instead of waiting.

ALTER DATABASE [DBNAME] SET ENABLE_BROKER WITH ROLLBACK IMMEDIATE
初心未许 2024-09-07 00:09:47

在 Sql Server 2012 中,您可以转到“属性”->“属性”。选项->服务代理

启用服务代理

In Sql Server 2012 you can go to Properties-> Options -> Service Broker

Enable Service Broker

噩梦成真你也成魔 2024-09-07 00:09:47

好的,如果您的备份被禁用或您需要恢复备份(这似乎会禁用它),请执行以下操作。

只要运行这个脚本,它就会杀死数据库正在使用的所有进程(为什么你不能在 2008 年手动杀死进程,不像 2005 年),然后设置代理

USE master
go

DECLARE @dbname sysname

SET @dbname = 'YourDBName'

DECLARE @spid int
SELECT @spid = min(spid) from master.dbo.sysprocesses where dbid = db_id(@dbname)
WHILE @spid IS NOT NULL
BEGIN
EXECUTE ('KILL ' + @spid)
SELECT @spid = min(spid) from master.dbo.sysprocesses where dbid = db_id(@dbname) AND spid > @spid
END


ALTER DATABASE @dbname SET ENABLE_BROKER

ok here is how to do this if yours is disabled or you need to restore a backup, which seems to disable it.

just run this script, it will kill all the process's that a database is using (why you carnt in 2008 manually kill process's unlike 2005 is beyond me) and then set the broker

USE master
go

DECLARE @dbname sysname

SET @dbname = 'YourDBName'

DECLARE @spid int
SELECT @spid = min(spid) from master.dbo.sysprocesses where dbid = db_id(@dbname)
WHILE @spid IS NOT NULL
BEGIN
EXECUTE ('KILL ' + @spid)
SELECT @spid = min(spid) from master.dbo.sysprocesses where dbid = db_id(@dbname) AND spid > @spid
END


ALTER DATABASE @dbname SET ENABLE_BROKER
因为看清所以看轻 2024-09-07 00:09:47

必须删除与数据库的所有连接,并且具有用于启用代理服务的权限的用户帐户。

以下内容是理想的(替换databasename):

     IF ((SELECT is_broker_enabled FROM sys.databases WHERE name = '%DATABASE_NAME%') = 1)
      BEGIN
        ALTER DATABASE %DATABASE_NAME% SET NEW_BROKER WITH ROLLBACK IMMEDIATE;
     END

    ALTER DATABASE %DATABASE_NAME% SET ENABLE_BROKER WITH ROLLBACK IMMEDIATE;

我还建议创建一个具有适当权限的新角色和用户帐户(替换数据库登录):

  --DBA creates a new role 
   if not exists (select 1 from sys.database_principals where name='sql_dependency_subscriber' and Type = 'R')
  begin
   EXEC sp_addrole 'sql_dependency_subscriber' 
  end

  --Minimum Required  Permissions needed for SQLDependancy Notification to work
   GRANT CREATE PROCEDURE to sql_dependency_subscriber;
   GRANT CREATE QUEUE to sql_dependency_subscriber; 
   GRANT CREATE SERVICE to sql_dependency_subscriber;
   GRANT REFERENCES on CONTRACT::[http://schemas.microsoft.com/SQL/Notifications/PostQueryNotification] to sql_dependency_subscriber ;
   GRANT VIEW DEFINITION TO sql_dependency_subscriber;

  --Minimum Required  Permissions  needed for SQLDependaney Notification to work
   GRANT SELECT to sql_dependency_subscriber;
   GRANT SUBSCRIBE QUERY NOTIFICATIONS TO sql_dependency_subscriber;
   GRANT RECEIVE ON QueryNotificationErrorsQueue TO sql_dependency_subscriber;
   GRANT REFERENCES on CONTRACT::[http://schemas.microsoft.com/SQL/Notifications/PostQueryNotification] to sql_dependency_subscriber ;
   EXEC sp_addrolemember 'sql_dependency_subscriber', '%DATABASE_LOGIN%';
   EXEC sp_addrolemember 'sql_dependency_subscriber', 'sqldp';

All connections to the DB must be be dropped and user account with permissions used to enable the broker service.

The following would be ideal (replace databasename):

     IF ((SELECT is_broker_enabled FROM sys.databases WHERE name = '%DATABASE_NAME%') = 1)
      BEGIN
        ALTER DATABASE %DATABASE_NAME% SET NEW_BROKER WITH ROLLBACK IMMEDIATE;
     END

    ALTER DATABASE %DATABASE_NAME% SET ENABLE_BROKER WITH ROLLBACK IMMEDIATE;

Also I suggest creating a new role and user account with the proper permissions (replace database login):

  --DBA creates a new role 
   if not exists (select 1 from sys.database_principals where name='sql_dependency_subscriber' and Type = 'R')
  begin
   EXEC sp_addrole 'sql_dependency_subscriber' 
  end

  --Minimum Required  Permissions needed for SQLDependancy Notification to work
   GRANT CREATE PROCEDURE to sql_dependency_subscriber;
   GRANT CREATE QUEUE to sql_dependency_subscriber; 
   GRANT CREATE SERVICE to sql_dependency_subscriber;
   GRANT REFERENCES on CONTRACT::[http://schemas.microsoft.com/SQL/Notifications/PostQueryNotification] to sql_dependency_subscriber ;
   GRANT VIEW DEFINITION TO sql_dependency_subscriber;

  --Minimum Required  Permissions  needed for SQLDependaney Notification to work
   GRANT SELECT to sql_dependency_subscriber;
   GRANT SUBSCRIBE QUERY NOTIFICATIONS TO sql_dependency_subscriber;
   GRANT RECEIVE ON QueryNotificationErrorsQueue TO sql_dependency_subscriber;
   GRANT REFERENCES on CONTRACT::[http://schemas.microsoft.com/SQL/Notifications/PostQueryNotification] to sql_dependency_subscriber ;
   EXEC sp_addrolemember 'sql_dependency_subscriber', '%DATABASE_LOGIN%';
   EXEC sp_addrolemember 'sql_dependency_subscriber', 'sqldp';
救星 2024-09-07 00:09:47

我们可以通过 ALTER DATABASE 语句以及数据库属性 > 来启用 Broker 服务。选项>服务经纪人>代理启用:对/错。

但在启用代理时出现问题:进程正在与特定数据库一起运行,我们必须杀死这些进程,只有在此之后我们才能启用代理服务。 @JGilmartin 的回答将完美地工作,但请确保它会杀死数据库的所有正在进行的进程,在生产中使用此脚本之前,请检查流量或进程的严重性。

We can enable Broker services by ALTER DATABASE statement and also from Database properties > Options > Service Broker > Broker Enable: True/False.

But issue here while enabling the broker: processes are running with the specific database we have to kill those and only after that we can enable broker service. answered by @JGilmartin will perfectly work, but make sure it'll kill all ongoing processes of your database, before using this script on production kindly check traffic or severity of process.

国粹 2024-09-07 00:09:47
  1. Service Broker 输出错误。图片1

  2. 要修复此问题,请使用 SSMS 启用 Service Broker,图 2

  3. 设置为 true 后,Service Broker 错误现在应该消失了,请参阅我的图 3

在此处输入图像描述
输入图片此处描述
输入图片此处描述

  1. Error Outputted by Service Broker. Image 1

  2. To Fix, enable Service broker using SSMS, Image 2

  3. After Setting to true Service Broker error should gone now, see my Image 3

enter image description here
enter image description here
enter image description here

老娘不死你永远是小三 2024-09-07 00:09:47

如果代理已启用,请尝试以下操作:

ALTER DATABASE <DBName> SET NEW_BROKER WITH ROLLBACK IMMEDIATE;

If the broker is already enabled try this:

ALTER DATABASE <DBName> SET NEW_BROKER WITH ROLLBACK IMMEDIATE;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文