G
Guest
hi all,
im trying to establish whether i have a race condition or critical section
in the following. i have a dataaccess class that continually retireves a
table from a sqlserver (which may be slow). it provides a cached copy of
this table to clients, for them to read from (note: there are no writes).
the idea being if a particular table retrival takes time, the class would
still provide a cached copy.
now the code:
public abstract class DataAccess
{
protected DataTable cached;
protected int repeattime;
private DataSet ds;
public UpdatableDataAccess(string s, string db):base(s, db)
{
//create connection initialise dataset
}
protected DataTable fillDataSet(string tablename, SqlCommand sqlcom)
{
SqlDataAdapter mysda = new SqlDataAdapter(sqlcom);
if (ds.Tables.Contains(tablename))
{
ds.Tables[tablename].Clear();
}
sqlc.Open();
mysda.Fill(ds, tablename);
sqlc.Close();
return ds.Tables[tablename].Copy();
}
public void updateTable()
{
while(true)
{
SqlCommand sqlcom = new SqlCommand(updatequery, sqlc);
DataTable holding = fillDataSet(tablename, sqlcom);
//critical section, i think
cached = holding;
//end critical
Thread.Sleep(repeattime * 1000);
}
}
public DataTable getLatestTable()
{
//critical section, i think
return cached;
//end critical
}
}
have i indicated the correct critical sections? if so, do i need to lock /
tryenter around them? i assume that the copy assignment to the cached field
is atomic, so there shouldnt be any problem, right? clients would use
"getLatestTable()" to get a table, while the thread method would be
"updateTable()". this is my first real MT app, so i think im missing
something!
Spammy
im trying to establish whether i have a race condition or critical section
in the following. i have a dataaccess class that continually retireves a
table from a sqlserver (which may be slow). it provides a cached copy of
this table to clients, for them to read from (note: there are no writes).
the idea being if a particular table retrival takes time, the class would
still provide a cached copy.
now the code:
public abstract class DataAccess
{
protected DataTable cached;
protected int repeattime;
private DataSet ds;
public UpdatableDataAccess(string s, string db):base(s, db)
{
//create connection initialise dataset
}
protected DataTable fillDataSet(string tablename, SqlCommand sqlcom)
{
SqlDataAdapter mysda = new SqlDataAdapter(sqlcom);
if (ds.Tables.Contains(tablename))
{
ds.Tables[tablename].Clear();
}
sqlc.Open();
mysda.Fill(ds, tablename);
sqlc.Close();
return ds.Tables[tablename].Copy();
}
public void updateTable()
{
while(true)
{
SqlCommand sqlcom = new SqlCommand(updatequery, sqlc);
DataTable holding = fillDataSet(tablename, sqlcom);
//critical section, i think
cached = holding;
//end critical
Thread.Sleep(repeattime * 1000);
}
}
public DataTable getLatestTable()
{
//critical section, i think
return cached;
//end critical
}
}
have i indicated the correct critical sections? if so, do i need to lock /
tryenter around them? i assume that the copy assignment to the cached field
is atomic, so there shouldnt be any problem, right? clients would use
"getLatestTable()" to get a table, while the thread method would be
"updateTable()". this is my first real MT app, so i think im missing
something!
Spammy