【疑难杂症】sqlCacheDependency 不起作用

故障环境

  • Windows 7 Ultimate
  • SQL Server 2008 R2
  • Visual Studio 2010(ASP.NET Development Server)

 

故障现象

  1. 启动 Web 应用程序,第一次打开页面时,通过 SQL Profiler 可以监视到向数据库发出的 SQL 命令。
  2. 第二次打开页面时,通过 SQL Profiler 可以监视到未向数据库发出的 SQL 命令,此时数据从缓存中读出。
  3. 修改数据库对应表中的数据。
  4. 第三次打开页面时,数据仍是第一次打开时的数据,通过 SQL Profiler 可以监视到未向数据库发出的 SQL 命令。

 

原因分析

  1. 已执行 alter database DbName set enable_broker,并且执行 select * from sys.databases 后,看到 is_broker_enabled 值为1。
  2. Web.config 中已存在 <caching><sqlCacheDependency enabled="true"/></caching>。
  3. Global.asax.cs 中已存在 SqlDependency.Start(connectionString); 和 SqlDependency.Stop(connectionString);。
  4. 页面未出现异常,加断点也未见异常,在 Windows 事件日志中大量的重复信息:
    “在目标队列中对消息进行排队时出现异常。错误: 15517,状态: 1。Cannot execute as the database principal because the principal "dbo" does not exist, this type of principal cannot be impersonated, or you do not have permission.”。
  5. 看来问题是出在数据库的“所有者”上,因为数据库是通过备份文件恢复的,而备份文件是从其他电脑上备份出来的。

 

解决方法

  1. 打开 Microsoft SQL Server Management Studio。
  2. 右击数据库节点,选择“属性”。
  3. 选择“文件”页,在所有者中设置正确的账号。

 

参考资源

http://stackoverflow.com/questions/7427026/sqlcachedependency-not-working 

posted on 2012-10-06 18:00  CsharpStyle  阅读(715)  评论(0编辑  收藏  举报