Hi everybody!
We have an app that caches information about many connections (not the actual alive connection but the provider factory for them).
We call a static method "Instance" that instantiates the private object and adds it to the cache.
public sealed class DbFab
{
private static object mioLockCheck = new object();
public static DbFab Instance
{
get
{
// try Get from Cache
DbFab lioDbFab = HttpRuntime.Cache["DbFab"] as DbFab;
if (lioDbFab == null)
{
lock (mioLockCheck)
{
lioDbFab = HttpRuntime.Cache["DbFab"] as DbFab;
if (lioDbFab == null)
{
// Creation of class
lioDbFab = new DbFab();
HttpRuntime.Cache.Insert("DbFab", lioDbFab);
}
}
}
return lioDbFab;
}
}
// private Constructor
private DbFab()
{
lioTc.TraceEvent(TraceEventType.Information, 0, "{0} T:{<thread>} P:{<process>} DbFab Created!);
...
// initialization...
...
}
}
In production, under IIS (w03 r2 / 64), the constructor is being called many times, from different threads or even from the same thread, the trace log looks like:
10:18:17.04007 T:5 P:7116 DbFab Created
10:19:28.04098 T:8 P:7116 DbFab Created
10:39:46.32220 T:8 P:7116 DbFab Created
10:44:51.76361 T:1 P:7116 DbFab Created
...
The dlls are compiled with the DEBUG and TRACE symbols defined. There are 5 dlls calling the dll that hosts the DbFab class.
In the ideal world, the DbFab should be called once and kept in cache. Any ideas why this is not happening?
Thanks
Dan
BTW: before we were using a singleton pattern with a static accesor and private constructor, but the same problem happened.
We have an app that caches information about many connections (not the actual alive connection but the provider factory for them).
We call a static method "Instance" that instantiates the private object and adds it to the cache.
public sealed class DbFab
{
private static object mioLockCheck = new object();
public static DbFab Instance
{
get
{
// try Get from Cache
DbFab lioDbFab = HttpRuntime.Cache["DbFab"] as DbFab;
if (lioDbFab == null)
{
lock (mioLockCheck)
{
lioDbFab = HttpRuntime.Cache["DbFab"] as DbFab;
if (lioDbFab == null)
{
// Creation of class
lioDbFab = new DbFab();
HttpRuntime.Cache.Insert("DbFab", lioDbFab);
}
}
}
return lioDbFab;
}
}
// private Constructor
private DbFab()
{
lioTc.TraceEvent(TraceEventType.Information, 0, "{0} T:{<thread>} P:{<process>} DbFab Created!);
...
// initialization...
...
}
}
In production, under IIS (w03 r2 / 64), the constructor is being called many times, from different threads or even from the same thread, the trace log looks like:
10:18:17.04007 T:5 P:7116 DbFab Created
10:19:28.04098 T:8 P:7116 DbFab Created
10:39:46.32220 T:8 P:7116 DbFab Created
10:44:51.76361 T:1 P:7116 DbFab Created
...
The dlls are compiled with the DEBUG and TRACE symbols defined. There are 5 dlls calling the dll that hosts the DbFab class.
In the ideal world, the DbFab should be called once and kept in cache. Any ideas why this is not happening?
Thanks
Dan
BTW: before we were using a singleton pattern with a static accesor and private constructor, but the same problem happened.