Groups | Blog | Home
all groups > asp.net caching > january 2008 >

asp.net caching : SqlDependency with Notification : thread synchrnization issue


Joe Abou Jaoude
1/29/2008 12:00:11 AM


Hi,

I m trying to use the SqlDependency with the Notification feature of SQL
SERVER 2005.
I load the data from the database in a Shared datatable (if the
datatable is empty).
When SQL SERVER notifies my app that a change has been made I clear the
datatable.
The code works fine, however I noticed that without a thread
synchrnization it can cause a problem.
Here's the code:
Dim dt As DataTable = MyDependency.GetCache()
GridView1.DataSource = dt
GridView1.DataBind()

MyDependency.GetCache() returns the Shared datatable.
If a change is made to the data while it's being bind a problem occurs.
To resolve the problem I tried to use the Monitor class as follow:

Dim dt As DataTable = MyDependency.GetCache()
Monitor.Enter(dt)
GridView1.DataSource = dt
GridView1.DataBind()
Monitor.Exit(dt)

However this method didn't prevent the datatable to be cleared by the
notification thread.

What I m doing wrong and what's the right way to resolve this issue.

Thank you.




Ravi
1/29/2008 9:13:02 PM
Hi Joe,
"However this method didn't prevent the datatable to be cleared by
the
notification thread." This is anyways the expected behaviour.
"What I m doing wrong and what's the right way to resolve this issue."
Well it depends. If you want your cache items to beevicted at any case then
leave it as it is. If its ok to have little bit stale data then create a copy
of datatable in your GetCache() method and return it. This will prevent
getting exceptions while DataBinding happens.

Hope this helps.



[quoted text, click to view]
AddThis Social Bookmark Button