代码之家  ›  专栏  ›  技术社区  ›  Greg B

为什么我的sqlpendency不启动

  •  3
  • Greg B  · 技术社区  · 14 年前

    我有一个运行在MS SQL Server 2005上的数据库和一个ASP.NET 3.5 Web应用程序。

    数据库包含一个产品目录,我用它把“页面”输入运行在web应用程序中的CMS。创建页面后,应用程序会缓存它们,因此我需要通知应用程序此更改,以便它可以重新创建页面对象。

    CMS使用自己的缓存,因此不能使用sqlcacheDependency来执行此任务。

    当我启动应用程序时,会有一个用户出现在

    select * from sys.dm_qn_subscriptions
    

    但只要我向表中添加一些数据,订阅服务器就会消失,并且onchanged事件永远不会触发。

    在我的启动代码中,我有以下内容

    // Ensure the database is setup for notifications
    SqlCacheDependencyAdmin.EnableNotifications(DataHelper.ConnectionString);
    SqlCacheDependencyAdmin.EnableTableForNotifications(DataHelper.ConnectionString, "Category");
    SqlCacheDependencyAdmin.EnableTableForNotifications(DataHelper.ConnectionString, "Product");
    
    if (!SqlDependency.Start(DataHelper.ConnectionString, SqlDependencyQueueName))
         throw new Exception("Something went wrong");
    
    string queueOptions = string.Format("service = {0}", SqlDependencyQueueName);
    using (var connection = new SqlConnection(DataHelper.ConnectionString))
    {
         connection.Open();
         using (SqlCommand command = new SqlCommand(@"SELECT [CategoryID],[Name]
                       FROM [dbo].[Category]", connection))
         {
               // Create a dependency and associate it with the SqlCommand.
               dependency = new SqlDependency(command, queueOptions, 0);
    
                // Subscribe to the SqlDependency event.
                dependency.OnChange += new OnChangeEventHandler(OnDependencyChange);
    
                command.ExecuteReader().Close();
            }
        }
    
    // ...
    
    void OnDependencyChange(object sender, SqlNotificationEventArgs e)
    {
        Debugger.Break(); // This never hits
        this.ReloadData();
    }
    
    1 回复  |  直到 14 年前
        1
  •  12
  •   Remus Rusanu    14 年前

    检查数据库 sys.transmission_queue . 很可能您的通知会在那里保留,因为它们无法送达。这个 transmission_status 会有一个解释为什么会这样。有关更详细的故障排除指南,请参阅 Troubleshooting Dialogs .

    最常见的问题是由于孤立数据库dbo所有权不满足基础设施要求而导致的,可以通过以下方式解决:

    ALTER AUTHORIZATION ON DATABASE::<dbname> TO [sa];
    

    然而,解决方案取决于具体情况,取决于实际问题,如上所述。