Sql server 7.0/2000下 SqlCacheDependency使用轮询的方式进行缓存失效检查, 虽然ms说对服务器压力不大, 但还是有一些的, 而且对于不常改动的混存内容无休止的轮询感觉有点浪费, 不很经济.

Sql server 2005/2008下增加使用查询通知方式进行缓存失效检查, 它通过Sql Server内部的消息队列进行异步通知, 这样就大大减轻了服务器的压力, 实现的很经济, 下面就是具体的步骤:

1.检测是否已经启用Service Broker

Select DATABASEpRoPERTYEX('数据库名称','IsBrokerEnabled')  -- 1 表示已经启用 0 表示没有启用

2.启用Service Broker

ALTER DATABASE 数据库名称 SET ENABLE_BROKER;

注意:如果执行此语句处于假死状态,请重启(restart)数据库,然后什么都别做, 先执行上面启用Service Broker这个语句就行了!

3.给您的数据库访问帐号授予权限

GRANT SUBSCRIBE QUERY NOTIFICATIONS TO test

注意:这一步非常重要, 如果没有权限, 数据库改变的通知将无法接收, cache永远都不会被刷新,我开始是用的sa帐号,死活都不刷新,花了我两天时间调试这个问题, 还是无法给sa授此权限(ms禁止), 所以,换个数据库访问帐号即可.

4.检查是否有以前的缓存错误

隐藏行号 复制代码 ? 这是一段程序代码。
  1. USE [master]
        
  2. GO
        
  3. ALTER DATABASE Northwind SET  SINGLE_USER WITH ROLLBACK IMMEDIATE
        
  4. GO
        
  5. ALTER DATABASE Northwind SET  SINGLE_USER
        
  6. GO
        
  7. ALTER DATABASE Northwind SET ENABLE_BROKER
        
  8. GO
        
  9. ALTER DATABASE Northwind SET  MULTI_USER
        
  10. GO
        

.src_container{background-color:#e7e5dc; width:99%; overflow:hidden; margin:12px 0 12px 0 !important; padding:0px 3px 3px 0px}
.src_container .titlebar{ background-color:#d4dfff; border:1px solid #4f81bd; border-bottom:0; padding:3px 24px; margin:0; width:auto; line-height:120%; overflow:hidden; text-align:left; font-size:12px}
.src_container .toolbar{ display:inline; font-weight:normal; font-size:100%; float:right; cursor:hand; color:#00f; text-align:left; overflow:hidden}
.toolbar span.button{ display:inline; font-weight:normal; font-size:100%; cursor:hand; color:#00f; text-align:left; overflow:hidden; cursor:pointer;}
.src_container div.clientarea{ background-color:white; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; height:auto; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys",courier,monospace,serif}
.src_container ol.mainarea{ padding:0 0 0 52px; margin:0; background-color:#f7f7ff !important}
.number_show{ padding-left:52px !important; list-style:decimal outside !important}
.number_show li{ list-style:decimal outside !important; border-left:1px dotted #4f81bd}
.number_hide{ padding-left:0px !important; list-style-type:none !important}
.number_hide li{ list-style-type:none !important; border-left:0px}
ol.mainarea li{ display:list-item !important; font-size:12px !important; margin:0 !important; line-height:18px !important; padding:0 0 0 0px !important; background-color:#f7f7ff !important; color:#4f81bd}
ol.mainarea li pre{color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important}
.linewrap ol.mainarea li pre{white-space:pre-wrap; white-space:-moz-pre-wrapwhite-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word}
ol.mainarea li pre.alt{ background-color:#f7f7ff !important}

5、启动SqlDependency监听.

在Global.ascx里的Application_Start和Application_End里增加如下代码:

隐藏行号 复制代码 ? 这是一段程序代码。
  1. void Application_Start(object sender, EventArgs e)
    
  2. {
    
  3.     string connString2 = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["NorthwindConnectionString1"].ConnectionString;
    
  4.     System.Data.SqlClient.SqlDependency.Start(connString2);
    
  5. }
    
  6. 
    
  7. void Application_End(object sender, EventArgs e)
    
  8. {
    
  9.     string connString2 = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["NorthwindConnectionString1"].ConnectionString;
    
  10.     System.Data.SqlClient.SqlDependency.Stop(connString2);
    
  11. }
    

.src_container{background-color:#e7e5dc; width:99%; overflow:hidden; margin:12px 0 12px 0 !important; padding:0px 3px 3px 0px}
.src_container .titlebar{ background-color:#d4dfff; border:1px solid #4f81bd; border-bottom:0; padding:3px 24px; margin:0; width:auto; line-height:120%; overflow:hidden; text-align:left; font-size:12px}
.src_container .toolbar{ display:inline; font-weight:normal; font-size:100%; float:right; cursor:hand; color:#00f; text-align:left; overflow:hidden}
.toolbar span.button{ display:inline; font-weight:normal; font-size:100%; cursor:hand; color:#00f; text-align:left; overflow:hidden; cursor:pointer;}
.src_container div.clientarea{ background-color:white; border:1px solid #4f81bd; margin:0; width:auto !important; width:100%; height:auto; overflow:auto; text-align:left; font-size:12px; font-family: "Courier New","Consolas","Fixedsys",courier,monospace,serif}
.src_container ol.mainarea{ padding:0 0 0 52px; margin:0; background-color:#f7f7ff !important}
.number_show{ padding-left:52px !important; list-style:decimal outside !important}
.number_show li{ list-style:decimal outside !important; border-left:1px dotted #4f81bd}
.number_hide{ padding-left:0px !important; list-style-type:none !important}
.number_hide li{ list-style-type:none !important; border-left:0px}
ol.mainarea li{ display:list-item !important; font-size:12px !important; margin:0 !important; line-height:18px !important; padding:0 0 0 0px !important; background-color:#f7f7ff !important; color:#4f81bd}
ol.mainarea li pre{color:black; line-height:18px; padding:0 0 0 12px !important; margin:0em; background-color:#fff !important}
.linewrap ol.mainarea li pre{white-space:pre-wrap; white-space:-moz-pre-wrapwhite-space:-pre-wrap; white-space:-o-pre-wrap; word-wrap:break-word}
ol.mainarea li pre.alt{ background-color:#f7f7ff !important}

6、主体程序

webfrom1: 单表缓存

webfrom2: LINQ TO SQL 缓存

webfrom3: LINQ TO SQL 多表缓存

【缓存】Sql Server 2005/2008 SqlCacheDependency查询通知的使用总结的更多相关文章

  1. 浅析SQL Server 2005中的主动式通知机制

    一.引言 在开发多人同时访问的Web应用程序(其实不只这类程序)时,开发人员往往会在缓存策略的设计上狠下功夫.这是因为,如果将这种环境下不常变更的数据临时存放在应用程序服务器或是用户机器上的话,可以避 ...

  2. SQL SERVER 2005/2008 中关于架构的理解(二)

    本文上接SQL SERVER 2005/2008 中关于架构的理解(一)      架构的作用与示例 用户与架构(schema)分开,让数据库内各对象不再绑在某个用户账号上,可以解决SQL SERVE ...

  3. SQL SERVER 2005/2008 中关于架构的理解(一)

    SQL SERVER 2005/2008 中关于架构的理解(一) 在一次的实际工作中碰到以下情况,在 SQL SERVER 2008中,新建了一个新用户去访问几张由其他用户创建的表,但是无法进行查询, ...

  4. 【转】SQL SERVER 2005/2008 中关于架构的理解

    在一次的实际工作中碰到以下情况,在 SQL SERVER 2008中,新建了一个新用户去访问几张由其他用户创建的表,但是无法进行查询,提示“对象名'CustomEntry' 无效.”.当带上了架构名称 ...

  5. 怎样修改SQL Server 2005/2008的系统存储过程(转)

    我们知道,SQL Server 2005/2008的系统存储过程在正常情况下是无法直接修改的. 尽管本文是介绍怎样修改它的,但在这里,我还是建议大家尽量不要去修改它.(好像有点绕哈...) OK,闲话 ...

  6. 监控 SQL Server (2005/2008) 的运行状况

    Microsoft SQL Server 2005 提供了一些工具来监控数据库.方法之一是动态管理视图.动态管理视图 (DMV) 和动态管理函数 (DMF) 返回的服务器状态信息可用于监控服务器实例的 ...

  7. PHP连接Microsoft SQL Server 2005/2008

    PHP自带的MSSQL扩展php_mssql.dll原来是给SQL Server 2000用的,难怪连接不上2008?! -_-!!要使用SQL Server 2005以上版本,就要用到微软为PHP提 ...

  8. SQL Server 2005,2008 正则表达式 替换函数应用详解

    CREATE function dbo.regexReplace ( @source ntext, --原字符串 ), --正则表达式 ), --替换值 , --是否是全局替换 --是否忽略大小写 ) ...

  9. Sql Server 2005/2008数据库被标记为“可疑”/“质疑”的问题

    日常对Sql Server 2005关系数据库进行操作时,有时对数据库(如:Sharepoint网站配置数据库名Sharepoint_Config)进行些不正常操作如数据库在读写时而无故停止数据库,从 ...

随机推荐

  1. Form_Form Builder本地部署运行的实现(案例)

    2014-08-09 Created By BaoXinjian

  2. HDU 1011 Starship Troopers 树形DP 有坑点

    本来是一道很水的树形DP题 设dp[i][j]表示,带着j个人去攻打以节点i为根的子树的最大收益 结果wa了一整晚 原因: 坑点1: 即使这个节点里面没有守卫,你如果想获得这个节点的收益,你还是必须派 ...

  3. hdu 1561 The more, The Better 背包型树形DP 简单题

    The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  4. vim 学习日志(1):剪切,复制,粘贴,删除,撤销

    一:光标命令     左h 上j 下k 右l :goto n 表示跳转到文本的第n个字符 :n 表示跳到第n行 nG n为行数,该命令立即使光标跳到指定行:n为空,光标跳到文件最后一行. Ctrl+G ...

  5. 转-Android微信支付

    http://blog.fangjie.info/android微信支付/ Android微信支付 2014-08-09 一.使用微信官方的提供的demo里的appid等 1.微信接口上手指南:(从“ ...

  6. ylbtech-Unitity-cs:传递的字符串中数字字符的数目

    ylbtech-Unitity-cs:传递的字符串中数字字符的数目 1.A,效果图返回顶部   1.B,源代码返回顶部 1.B.1, using System; namespace Functions ...

  7. Python深入05 装饰器

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 装饰器(decorator)是一种高级Python语法.装饰器可以对一个函数.方法 ...

  8. WCF Windows Service Using TopShelf and ServiceModelEx z

    http://lourenco.co.za/blog/2013/08/wcf-windows-service-using-topshelf-and-servicemodelex/ There are ...

  9. .net 程序集自动生成版本号

    一. 版本号自动生成方法 只需把 AssemblyInfo.cs文件中的 [assembly:AssemblyVersion("1.0.0.0")]改成 [assembly:Ass ...

  10. Java中的观察者模式

    让一个类能够被观察,则该类需要继承java.util.Observable类. 要让一个类成为观察者,则该类需要实现java.util.Observable接口. 让观察者和被观察者建立联系通过Obs ...