[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

Static Member starts failing...

akshay

4/6/2006 7:18:00 PM

I facing a Static Member become Null on second call every now and then. -

I have one dataset made as Static Member. [_dsCacheHolder]
Each call to a function only returns a COPY Dataset.Copy() of the static
variable.
[public static PA_PropertyStructure GetCacheStructure()]
GetCacheStructure checks if Static member is null if yes then fills it up.
and returns a copy of the DS.

However I am observing a strange thing happening every now and then. As below:
1st time I call GetCacheStructure() - It loads up the static variable
dsCacheHolder and works fine.
Call again to GetCacheStructure() does this:
Finds _dsCacheHolder to be NULL.
Loads up _dsCacheHolder and as soon as it tries to return a copy of
_dsCacheHolder it finds that the variable is still NULL.

This code works fine almost 95% of the times - however on some days when
load to the server is low - it starts failing...

Also note i can re-create this issue / it was observed only on Windows 2003
and XP workstations. NT2000 servers works great, never seen any issue with
them.

Please help I m pulling my hair to get it fixed.


Code:

Tracer.TraceEnter();
savePoints = "entered try | ";
if (isCacheNotValid())
{
savePoints += "entered Cache Invalid 1 | ";
lock(_LockDummy)
{
savePoints += "entered Lock Dummy | ";
// Check once again ...
// 1. Cause the Thread ONE might have populated it _dsCacheHolder
while Thread TWO was in WAIT MODE...
// 2. Thread TWO might un-necessary re-build this Cache....
if (isCacheNotValid())
{
savePoints += "entered Cache Invalid 2 | ";

PerfCache._dsCacheHolderOut = new PA_PropertyStructure();

// DataSet dummyDS = new DataSet();
PerfCache._dsCacheHolderOut.EnforceConstraints = false;

PerfCache._dsCacheHolderOut.ReadXml(OptionsFilePath,
XmlReadMode.IgnoreSchema);

// _dsCacheHolderOut.Merge(dummyDS,true);
_dsCacheHolderOut.EnforceConstraints = true;
// dummyDS = null;

// Cache Dependency....
CacheDependency _Depends = new CacheDependency( OptionsFilePath );

// Cache Dependency....
_publishedCache.Insert("FileChanged", "false", _Depends,
Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration,
CacheItemPriority.Normal,
onOptionFileChange );
savePoints += "completed the save | ";
return _dsCacheHolderOut.Copy();



1 Answer

Alvin Bruney [ASP.NET MVP]

4/7/2006 3:58:00 PM

0

You have a bug in your code. Static objects are not threadsafe. At the point
you start replenishing the cache, other threads/requests will invoke the
replenish function since they too find the cache empty possibly resulting in
corrupted data. A better approach is to stick the cache object in cache and
not make it a static variable. The benefit gained is that your routine is
now threadsafe. Another thing, for static objects to suddenly become null
assuming that nothing else touches your code, the app domain must have
unloaded. first place to start is the application event logs for recycling
events..

--
Warm Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ www.lulu.com/owc
Professional VSTO 2005 - Wrox/Wiley 2006
Blog: http://msmvps.com/bl...
-------------------------------------------------------



"akshay" <akshay@discussions.microsoft.com> wrote in message
news:AEAD2824-DFC5-4945-B740-ADA24224C54F@microsoft.com...
> I facing a Static Member become Null on second call every now and then. -
>
> I have one dataset made as Static Member. [_dsCacheHolder]
> Each call to a function only returns a COPY Dataset.Copy() of the static
> variable.
> [public static PA_PropertyStructure GetCacheStructure()]
> GetCacheStructure checks if Static member is null if yes then fills it up.
> and returns a copy of the DS.
>
> However I am observing a strange thing happening every now and then. As
below:
> 1st time I call GetCacheStructure() - It loads up the static variable
> dsCacheHolder and works fine.
> Call again to GetCacheStructure() does this:
> Finds _dsCacheHolder to be NULL.
> Loads up _dsCacheHolder and as soon as it tries to return a copy of
> _dsCacheHolder it finds that the variable is still NULL.
>
> This code works fine almost 95% of the times - however on some days when
> load to the server is low - it starts failing...
>
> Also note i can re-create this issue / it was observed only on Windows
2003
> and XP workstations. NT2000 servers works great, never seen any issue with
> them.
>
> Please help I m pulling my hair to get it fixed.
>
>
> Code:
>
> Tracer.TraceEnter();
> savePoints = "entered try | ";
> if (isCacheNotValid())
> {
> savePoints += "entered Cache Invalid 1 | ";
> lock(_LockDummy)
> {
> savePoints += "entered Lock Dummy | ";
> // Check once again ...
> // 1. Cause the Thread ONE might have populated it _dsCacheHolder
> while Thread TWO was in WAIT MODE...
> // 2. Thread TWO might un-necessary re-build this Cache....
> if (isCacheNotValid())
> {
> savePoints += "entered Cache Invalid 2 | ";
>
> PerfCache._dsCacheHolderOut = new PA_PropertyStructure();
>
> // DataSet dummyDS = new DataSet();
> PerfCache._dsCacheHolderOut.EnforceConstraints = false;
>
> PerfCache._dsCacheHolderOut.ReadXml(OptionsFilePath,
> XmlReadMode.IgnoreSchema);
>
> // _dsCacheHolderOut.Merge(dummyDS,true);
> _dsCacheHolderOut.EnforceConstraints = true;
> // dummyDS = null;
>
> // Cache Dependency....
> CacheDependency _Depends = new CacheDependency( OptionsFilePath );
>
> // Cache Dependency....
> _publishedCache.Insert("FileChanged", "false", _Depends,
> Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration,
> CacheItemPriority.Normal,
> onOptionFileChange );
> savePoints += "completed the save | ";
> return _dsCacheHolderOut.Copy();
>
>
>