Showing posts with label dependency. Show all posts
Showing posts with label dependency. Show all posts

Friday, March 30, 2012

Problem with SqlCacheDependency

I've set up a SqlCacheDependency in my Asp.Net application, but the dependency invalidates the cache immediately every time the page is hit.

I think the problem may be with my SQL Query, but it seems to me to meet the requirements on the Special Considerations When Using Query Notifications page on MSDN. Could someone take a look at this query and tell me if I've done something wrong? Thank you.

Here's my query:

Code Snippet

USE [chameleon]

GO

SET ANSI_NULLS ON

GO

SET ANSI_PADDING ON

GO

SET ANSI_WARNINGS ON

GO

SET CONCAT_NULL_YIELDS_NULL ON

GO

SET QUOTED_IDENTIFIER ON

GO

SET NUMERIC_ROUNDABORT OFF

GO

SET ARITHABORT ON

GO

ALTER PROCEDURE [dbo].[usp_customers_by_site_id]

@.site_id INT

AS

SELECT

customers.customer_id,

customers.name,

customers.po_prefix,

customers.dt_created,

customers.created_by AS auid,

customers.po_required

FROM dbo.customers

WHERE customers.site_id = @.site_id

AND customers.is_active = 1

and here's the code in my Asp.Net site where I try to use the SqlCacheDependency:

Code Snippet

public List<Customer> GetCustomersBySite(int siteID)

{

List<Customer> customers = new List<Customer>();

if (HttpRuntime.Cache["CustomersBySite" + siteID] != null){

customers = (List<Customer>)HttpRuntime.Cache["CustomersBySite" + siteID];

}

else

{

using (SqlCommand command = new SqlCommand("usp_customers_by_site_id", Connection)){

command.CommandType = CommandType.StoredProcedure;

command.Parameters.Add("@.site_id", SqlDbType.Int).Value = GetDBValue(siteID, false);

SqlCacheDependency dependency = new SqlCacheDependency(command);

try{

Connection.Open();

SqlDataReader reader;

reader = command.ExecuteReader(CommandBehavior.SingleResult);

while (reader.Read()){

customers.Add(PopulateCustomer(reader));

}

HttpRuntime.Cache.Insert("CustomersBySite" + siteID, customers, dependency, DateTime.Now.AddMinutes(Configuration.CacheDuration), System.Web.Caching.Cache.NoSlidingExpiration);

}

finally{

Connection.Close();

}

}

}

return customers;

}

My apologies. I posted this in the wrong forum.

-Brad

Monday, March 26, 2012

Problem with SQL Dependency

Hi -

I am trying to set up a SQL dependency using VS Studio 2005 and SQL Server 2005, Dev Ed. My problem is that the handler for the onchange event never gets called, and the SQL Service Broker statistics report shows it as disabled.

This is really vexing because this is what I have done:

The service broker was enabled using this:

use masteralterdatabase mydatabaseSET ENABLE_BROKER

which I can verify by using:select is_broker_enabled from sys.databases where name = ''mydatabase', which results 1.

I've also given the user the right permissions, I believe, like this:

use mydatabase

GRANT CREATE PROCEDURE TO myuser
GRANT CREATE QUEUE TO myuser
GRANT CREATE SERVICE TO myuser

This isn't doing me any good, it seems, although I took care to log in as myuser. The connection works, and I can read/write data from the db. However the following code does not seem to do anything:

Public Sub StartListening()If IsNothing(Dependency)Then Dim cAs New SqlClient.SqlCommand("SELECT Messages.ID, Messages.TimeInitiated, Messages.TimeToSend, Messages.TimeSent FROM(Messages)") Dependency =New SqlClient.SqlDependency(c)AddHandler Dependency.OnChange,AddressOf UpdateMessages SqlClient.SqlDependency.Start(LoadConnectionString)End If Messages = MessageTA.GetDataEnd Sub Public Sub UpdateMessages(ByVal senderAs Object,ByVal eAs System.Data.SqlClient.SqlNotificationEventArgs)Dim i = 1End Sub

Or at least my breakpoint in UpdateMessages is never reached.

ANY HELP OR IDEAS AS TO WHAT I AM MISSING IS HIGHLY APPRECIATED!!!

Thanks,

Oliver

Have you enabled both your database and tables for change notifiations?

Have a look at this article. It should help you.

http://www.ondotnet.com/pub/a/dotnet/2005/01/17/sqlcachedependency.html