首页 > 代码库 > 【缓存】Sql Server 2005/2008 SqlCacheDependency查询通知的使用总结

【缓存】Sql Server 2005/2008 SqlCacheDependency查询通知的使用总结

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     
<style type="text/css"> .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} </style><script language="javascript"> function CopyCode(key){var codeElement=null;var trElements=document.all.tags("ol");var i;for(i=0;i<trelements.length;++i){if(key.parentelement.parentelement.parentelement==trelements[i].parentelement.parentelement){codeelement=trelements[i];break}}if(codeelement!=null){var content="codeElement.innerText;if(window.clipboardData=http://www.mamicode.com/=null){window.alert(" 您的浏览器不支持脚本复制,请尝试手动复制。")}else{window.clipboardData.setData("Text",content);window.alert("源代码已经复制到剪贴板上。")}}}function" LineNumberVisible(key){var codeelement="null;var" trelements="document.all.tags(" ol");var" i;for( i="0;i<trElements.length;++i){if(key.parentElement.parentElement.parentElement==trElements[i].parentElement.parentElement){codeElement=trElements[i];break}}if(codeElement!=null){if(codeElement.className==" mainarea" number_hide"){codeElement. classname="mainarea number_show" ;key. innertext="隐藏行号" }else{codeElement. classname="mainarea number_hide" ;key. innertext="显示行号" }}}function ChangeIcon(key,isHover){if(isHover)key.style. color=‘red‘ ;else key.style. color=‘blue‘ }function CopyCode_CheckKey(key){if(window.event. keycode="=13)CopyCode(key)}function" AboutMe(){window.alert("本代码框由 CodePaste for Windows Live Writer 生成。\r\n\r\nAuthor: 范传根\r\nEmail: chuangen@live.cn\r\nWebsite: http://chuangen.name\r\nBlog: http://blog.csdn.net/chuangen");} </script>

 

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. void Application_End(object sender, EventArgs e)
  7. {
  8.     string connString2 = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["NorthwindConnectionString1"].ConnectionString;
  9.     System.Data.SqlClient.SqlDependency.Stop(connString2);
  10. }
<style type="text/css"> .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} </style><script language="javascript"> function CopyCode(key){var codeElement=null;var trElements=document.all.tags("ol");var i;for(i=0;i<trElements.length;++i){if(key.parentElement.parentElement.parentElement==trElements[i].parentElement.parentElement){codeElement=trElements[i];break}}if(codeElement!=null){var content=codeElement.innerText;if(window.clipboardData=http://www.mamicode.com/=null){window.alert("您的浏览器不支持脚本复制,请尝试手动复制。")}else{window.clipboardData.setData("Text",content);window.alert("源代码已经复制到剪贴板上。")}}}function LineNumberVisible(key){var codeElement=null;var trElements=document.all.tags("ol");var i;for(i=0;i<trElements.length;++i){if(key.parentElement.parentElement.parentElement==trElements[i].parentElement.parentElement){codeElement=trElements[i];break}}if(codeElement!=null){if(codeElement.className=="mainarea number_hide"){codeElement.className="mainarea number_show";key.innerText="隐藏行号"}else{codeElement.className="mainarea number_hide";key.innerText="显示行号"}}}function ChangeIcon(key,isHover){if(isHover)key.style.color=‘red‘;else key.style.color=‘blue‘}function CopyCode_CheckKey(key){if(window.event.keyCode==13)CopyCode(key)}function AboutMe(){window.alert("本代码框由 CodePaste for Windows Live Writer 生成。\r\n\r\nAuthor: 范传根\r\nEmail: chuangen@live.cn\r\nWebsite: http://chuangen.name\r\nBlog: http://blog.csdn.net/chuangen");} </script>

 

 

6、主体程序

 

webfrom1: 单表缓存

webfrom2: LINQ TO SQL 缓存

webfrom3: LINQ TO SQL 多表缓存

 

技术分享

【缓存】Sql Server 2005/2008 SqlCacheDependency查询通知的使用总结