hi, i am using following class to provide access to language resources in an asp.net application. i render the page for selected language by getting the text values from database. so i try to optimize fetching texts by caching them in a static datatable. However, i am not sure whether its always safe to read from tableResources that it may be re-created by UpdateResources function. I know GC will not release the object when its read by Rows.Find but i don't know much about GC. It may cause a deadlock or stuck GC, whatever else. (I think IL instructions are not atomic, unless the ones that compiled to a single CPU instruction). Please, help me to comprehend this.
public class Resources
{
public static DataTable tableResources;
public static object objSync = new object();
private PageLangs PageLang;
static Resources()
{
UpdateResources();
}
public Resources(PageLangs pageLang)
{
PageLang = pageLang;
}
public static void UpdateResources()
{
OleDbConnection con = ProjectLib.CreateDBConnection();
try
{
con.Open();
OleDbDataAdapter adap = new OleDbDataAdapter("SELECT Resource0,Resource1,Resource2,Resource3,Resource4,Resource5,Resource6,ResourceCode FROM Resources", con);
DataTable dt = new DataTable();
adap.Fill(dt);
adap.Dispose();
dt.PrimaryKey = new DataColumn[] { dt.Columns["ResourceCode"] };
// DataTable is thread-safe for multiple reads but not for writes so sync. it.
lock (objSync)
{
tableResources = dt;
}
}
catch
{
}
finally
{
ProjectLib.CloseDBConnection(con);
}
}
public string this[string resourceCode]
{
get
{
try
{
DataRow row = tableResources.Rows.Find(resourceCode);
if (row != null)
return row[(int)PageLang] as string;
else
return resourceCode;
}
catch
{
return null;
}
}
}
}