Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

SQL Server 2005 Query Notifications Tell .NET 2.0 Apps When Critical Data Changes : Page 6

.NET 2.0 and SQL Server 2005 combine in Query Notifications to notify applications when critical data changes occur and eliminate the need to repeatedly ask the database, "Has it changed yet?"


advertisement
Query Notification and ASPNET's Cache
In addition to the ADO.NET-based query notification, there is an entirely alternate set of tools built into ASP.NET 2.0 that build database cache invalidation into your Web site. The effect is, in some ways, similar to what you achieve with the middle tier caching samples above. This functionality is something that was done previously through a great deal of trickery using database polling, as detailed in Jeff Prosise's April 2003 Wicked Code column. Figure 2 shows the new queue and service created to be used with the QueryNotificationRequest

 
Figure 2: This image shows the new queue and service created by the T-SQL to be used with the QueryNotificationRequest.
System.Web.SqlCacheDependency is a class that you can use in code, similar to SqlDependency, but then add into the Response.Cache to enable cache invalidation based on database updates. An even simpler way to achieve this is through the <OutputCache> directive in the page's HTML. There are a few differences between these two methods.

  • Using SqlCacheDependency attaches the dependency to a specific SqlCommand.
  • Using <OutputCache> instructs the page to request a notification for any valid command that is called by the page.
  • You have more fine-grained control over the cache with the SqlCacheDependency.
  • <OutputCache> enables you to additionally create cache invalidation when using SQL 7.0 and SQL 2000. I will explain this in more detail below.
Here's an example of using SqlCacheDependency to request notification on a specific command.

Protected Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load LoadTime.Text = Now.ToLongTimeString Dim dep As SqlCacheDependency Using conn As New SqlConnection _ (ConfigurationManager.ConnectionStrings( _ "pubs").ConnectionString) Using cmd As New SqlCommand _ ("SELECT au_id,au_lname,au_fname " & _ "FROM dbo.authors where state='CA'", conn) 'create dependency and attach to command cmd.Notification = Nothing conn.Open() dep = New SqlCacheDependency(cmd) GridView1.DataSource = cmd.ExecuteReader GridView1.DataBind() End Using End Using 'add dependency to outputcache and configure Response.AddCacheDependency(dep) Response.Cache.SetValidUntilExpires(True) Response.Cache.SetExpires( _ DateTime.Now.AddSeconds(30)) Response.Cache.SetCacheability( _ HttpCacheability.Public) End Sub

In the preceding code, creating the object is similar to creating the SqlDependency object in ADO.NET. Then you tie the notification into the page's cache with Cache.AddDependency, setting the familiar caching properties. In this case, the cache is valid until it expires, which is either within 30 seconds or when a notification comes in, whichever happens first. When the page cache is invalidated, the next request for this page creates the page from scratch, forcing the data access to happen again.

Remember that you don't see SqlDependency.Start here because it is in the Global.asax.

Invalidating the cache with the <OutputCache> in the page's markup is even simpler. By using the new SqlDependency parameter of <OutputCache> and setting its value to CommandNotification, you are plugged right in to query notifications. Here is an example with only the minimal parameters being used.

<%@ OutputCache SqlDependency="CommandNotification" Duration="30" VaryByparam="none" %>

Any valid queries called from this page have a notification request registered in the Service Broker. The data access does not necessarily have to be done in the page's client-side or server-side code code-behind). You can call into another class or even another assembly for the data access and it will still use query notification requested by the page.

The SqlDependency parameter can also take other values, but only the CommandNotification value, used with SQL Server 2005, gives you the Service Broker Query Notification. ASP.NET 2.0 is also wired up to give cache invalidation from SQL 7 and SQL 2000. One way to do this is by using the value database-name:table-name (for example, pubs:authors) as the SqlDependency value. The effect is that a polling type of query is performed. There are more pieces necessary to make this work, but as it is outside of the scope of this article, I will point you to the "Implementing SQL Server 7/2000 Dependency Caching" explanation and example in the Visual Web Developer Guide on the ASP.NET Web site.

Query Notification in Transactions
Query Notifications do work inside of transactions. However, you will only get one notification per transaction regardless of the number of changes made to the database within that transaction.

SQL Server Express
Query Notifications can be used with SSE but the SSE database must be a named instance. Named instance is a setup option.

Additionally, the database that you want to use must be an attached database.

Although Query Notification is definitely one of the big new features in .NET 2.0, it still plays underdog to other exciting features such as generics or the UI magic in ASP.NET. Whether you use it to prevent the incessant re-querying for a drop down list of the 50 U.S. states or to manage the Web-based updates of millions of client computers, it can help you reduce overhead on your resources. In its simplest use—through the ASP.NET OutputCache directive—or by building a complex caching mechanism in your Web application's middle tier or Web service, Query Notification can be a powerful ally in creating extensible, responsive applications.



Julia Lerman is an independent consultant who has been designing and writing software applications for 20 years. She lives in Vermont where she runs the Vermont.NET User Group. Julia is well known in the .NET community as an INETA Board member, .NET MVP, ASPInsider, conference speaker, and prolific blogger. You can read Julia's blog at thedatafarm.com/blog.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date