[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.aspnet.caching

SqlDependency with Notification : thread synchrnization issue

Shawn Jackson

1/29/2008 8:00:00 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.




*** Sent via Developersdex http://www.develop... ***
1 Answer

Ravi

1/30/2008 5:13:00 AM

0

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.



"Joe Abou Jaoude" wrote:

>
>
> 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.
>
>
>
>
> *** Sent via Developersdex http://www.develop... ***
>