.NET Framework provides a set of prebuilt collection classes in System.Collections. Among of these classes, I have been using ArrayList and Hashtable intensively. So far there is no problem in ArrayList class, but I ever encountered an exception “InvalidOperationException, Hashtable insert failed. Load factor too high” when trying to insert an item to the Hashtable collection.
The application is a web-based application. The initial code was to fetch the records from database, populated it to the Hashtable and cache it using Cache class of System.Web.Caching for an amount of time, let say 30 minutes. The intention of this expiration time is to let the hashtable refreshed the new added items in database if there is any. There are about 1200++ items to put into the cache, the application will retrieve the item value based on the item key passed in. The code may look like this:
System.Web.Caching.Cache _cache = new System.Web.Caching.Cache(); ... public string Lookup(string strKey) { string strResult = string.Empty; if (_cache["DATA"] == null) { InitCache(); // get data from database and cache it } Hashtable ht = (Hashtable)_cache["DATA"]; if (ht[strKey] != null) strResult = ht[strKey].ToString(); return strResult; }
There was one problem with this approach, when there is a lookup for the item, and at the same time, the cache was reinitialized due to the expiration time, there was possibility that the lookup item couldn’t be found for specific lookup request.
The code was changed slightly, to used a static hashtable variable instead, so no more caching is required, as the static variable will stay in memory till the IIS is reset. The code may look like this:
System.Web.Caching.Cache _cache = new System.Web.Caching.Cache(); static Hashtable _ht = null; ... public string Lookup(string strKey) { string strResult = string.Empty; if (_ht == null) { InitCache(); // get data from database and populate to _ht variable } if (_ht[strKey] != null) strResult = _ht[strKe].ToString(); return strResult; }
Another problem was encountered again, the was possibility that the item couldn’t be retrieved again. Suspected this was due to more than one lookup requests at the initial time, the checking of _ht == null was not true when due to some items had been populated by other requests, but not fully populated.
Again, the code was revised, this time to add in an individual item fetching from database. The code may look like this:
System.Web.Caching.Cache _cache = new System.Web.Caching.Cache(); static Hashtable _ht = null; ... public string Lookup(string strKey) { string strResult = string.Empty; if (_ht == null) { InitCache(); // get data from database and populate to _ht variable } if (_ht[strKey] == null) { // get the particular key item from database only // and populate to _ht variable InitCache(strKey); } if (_ht[strKey] != null) strResult = _ht[strKe].ToString(); return strResult; }
The code worked fine for a while, but intermittently, the exception “InvalidOperationException, Hashtable insert failed. Load factor too high” occurred, bingo.
After googling a bit, I found a Microsoft Knowledge Base at http://support.microsoft.com/?kbid=831730, it states the cause as “Because the circumstances that cause this problem are complex, it is difficult to predict when this problem might occur. However, if you insert the same set of data in the same order again, the problem will occur again.”
And refer to The .NET Framework 1.1 Service Pack 1 List of fixed bugs at http://support.microsoft.com/?kbid=867460, this exception has been resolved. Athough the Service Pack 1 has been installed on development and production server, but intermittently the problem still occurs, so far only at the production server, It is something to find out.
Popularity: 4% [?]
RSS feed for comments on this post · TrackBack URI
Leave a reply