473,406 Members | 2,336 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

SqlCacheDependency immediately gets DependencyChanged

Hi

I set up a sql cache dependency, but always receive a "DependencyChanged" notification straight away (through CacheItemRemoved callback). Modifying the procedure didn't help me.
It used to work running on a SQL 2000 database with a SqlDataSource and a few things set in the configuration file.
The current setup is using SQL 2005 Express and caching in the datalayer/business objects.

Running profiler, I notice events ran by the SqlQueryNotificationService :

exec sp_executesql N'BEGIN CONVERSATION TIMER (''f31d830f-fc01-dc11-9742-00166f698a50'') TIMEOUT = 120; WAITFOR(RECEIVE TOP (1) message_type_name, conversation_handle, cast(message_body AS XML) as message_body from [SqlQueryNotificationService-103f7ce3-56c0-4c67-affb-bb320ed5687a]), TIMEOUT @p2;',N'@p2 int',@p2=60000

exec dbo.p_b2ex_get_workbooks

exec sp_executesql N'END CONVERSATION @p1; BEGIN CONVERSATION TIMER (''f31d830f-fc01-dc11-9742-00166f698a50'') TIMEOUT = 120; WAITFOR(RECEIVE TOP (1) message_type_name, conversation_handle, cast(message_body AS XML) as message_body from [SqlQueryNotificationService-103f7ce3-56c0-4c67-affb-bb320ed5687a]), TIMEOUT @p2;',N'@p2 int,@p1 uniqueidentifier',@p2=60000,@p1='F91D830F-FC01-DC11-9742-00166F698A50'


Any ideas about what's wrong or missing ?

Many thanks
Reinout

Code :

Global.asax :

void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
System.Data.SqlClient.SqlDependency.Start(Configur ationManager.ConnectionStrings["B2exDatabase"].ToString());
}

void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
System.Data.SqlClient.SqlDependency.Stop(Configura tionManager.ConnectionStrings["B2exDatabase"].ToString());
}

Business Object :

private void DataPortal_Fetch(FilterCriteria criteria)
{
RaiseListChangedEvents = false;
IsReadOnly = false;

WorkbookList wbList;

Cache cache = HttpRuntime.Cache;
wbList = cache["Workbooks"] as WorkbookList;

if (wbList == null)
{
SqlCacheDependency scd = null;

using (DataTable dt = B2exDAL.Instance.GetWorkbooks(ref scd))
{
DataTableReader dtr = dt.CreateDataReader();
SafeDataReader sdr = new SafeDataReader(dtr);

while (sdr.Read())
{
this.Add(WorkbookInfo.GetWorkbook(sdr));
}
}

wbList = this;

cache.Insert("Workbooks", wbList, scd, Cache.NoAbsoluteExpiration, new TimeSpan(0, 20, 0),
CacheItemPriority.NotRemovable, new System.Web.Caching.CacheItemRemovedCallback(Test)) ;
}

IsReadOnly = true;
RaiseListChangedEvents = true;
}

private void Test(string text, object sender, System.Web.Caching.CacheItemRemovedReason reason)
{
// Notify deletion from cache
}

DAL :

public override DataTable GetWorkbooks(ref System.Web.Caching.SqlCacheDependency scd)
{
DataTable workbooks = new DataTable();

using (SqlConnection conn = new SqlConnection(base.ConnectionString))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = PROC_GET_WORKBOOKS;

scd = new SqlCacheDependency(cmd);

SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = cmd;

adapter.Fill(workbooks);
}

return workbooks;
}
May 14 '07 #1
4 5813
kenobewan
4,871 Expert 4TB
Here is an article that may help:
SQL Express Dependency Caching
May 14 '07 #2
Apparantly I did nothing wrong in the code.

Since I was becoming kinda desperate, I changed my procedure to a hardcoded select statement without any other code. All of a sudden, it worked.
I found out that left joins are not allowed when using the notification service.

At the moment, I don't see another way than creating a custom query giving me an indication for changed records in one of the tables, and at that time invalidate the BO.
At least it seems to be better than polling the DB manually every few seconds.

Other workarounds are still welcome though !
May 14 '07 #3
Updated DAL code :


public override DataTable GetWorkbooks(ref System.Web.Caching.SqlCacheDependency scd)
{
DataTable workbooks = new DataTable();

using (SqlConnection conn = new SqlConnection(base.ConnectionString))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = PROC_GET_WORKBOOKS;

SqlCommand cmdDependency = new SqlCommand("SELECT <field> FROM <table>", conn);
scd = new SqlCacheDependency(cmdDependency);

SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = cmd;

adapter.Fill(workbooks);
}

return workbooks;
}
May 14 '07 #4
I had the same issue with a stored proc before, and after spending a few hours, I tried something that made it work.

Initially I was setting the command text to the name of the stored procedure, say, "GET_PUBLISHERS", and I used to get the notification immediately, causing my cache to get flushed. Then I added "dbo." in front of the stored proc name, like so: "dbo.GET_PUBLISHERS", and voila, it started to work!
Oct 4 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: stewart | last post by:
I've got the standard SqlCacheDependency working just fine , ie. I've defined (and encrypted) the connectionStrings section in the web.config, and I've also defined an an sqlCacheDependency in the...
5
by: Adrian Parker | last post by:
I've got the standard SqlCacheDependency working just fine , ie. I've defined (and encrypted) the connectionStrings section in the web.config, and I've also defined an an sqlCacheDependency in the...
5
by: Joel Barsotti | last post by:
I'm trying to get sql cache dependency working correctly. I've got sql server 2005 for my back end. I've don the "ALTER DATABASE dbName SET broker_enable" command. I made sure the user in my...
2
by: chris | last post by:
Hello all, Background: Using .NET 2.0, Sql Server 2005, SqlCacheDependency is utilized successfully against SqlCommand objects elsewhere in the code (in other words I have successful instances...
0
by: Jayender | last post by:
I read an article : MSDN help topic. This will get you on the right track. http://msdn2.microsoft.com/en-us/library/system.web.caching.sqlcachedependency.aspx and its really amazing .. I tried...
10
by: J055 | last post by:
Hi I've been trying out SqlCacheDependency using the ObjectDataSource and SQL Server 2005. It all works quite well with the minimum of configuration, e.g. <asp:ObjectDataSource...
2
by: mrashidsaleem | last post by:
Using the SQL 2005 "query notification" caching mechanism, I am caching three columns (UserId, UserName, Password) of a table called UserInfo in my web application cache. However, the cache gets...
1
by: mark4asp | last post by:
Help: SQLCacheDependency not working. When I step through my code with the debugger I notice that the condition below: (cacheItem == null) is always true. I have setup SQLCacheDependency...
1
by: | last post by:
Hey Guys, I have been struggling with this for few days. when i use SqlCacheDependency sqlDependency=new SqlCacheDependency("test","dbo.USStates"); where test is the name of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.