Is this a correct way of doing an optimistic update
Imagine two different users executing this code at the same time.
I can not use the CRecordset Update Delete methods for this.
// Check to see if version number has been incremented since this view read in
// the client data
CClientRecordSet rs(mConn);
rs.m_strFilter = "ID = ";
rs.m_strFilter += client->GetKey();
rs.m_strFilter +=" AND ";
rs.m_strFilter += "Version = ";
rs.m_strFilter += client->GetVersion();
// Assuming here, this is acting as a lock on tables referenced
mConn->BeginTrans();
rs.Open(CRecordset::snapshot, NULL, CRecordset::readOnly);
// No record match so version number incremented, some else has edited the
// record or it has now been deleted.
if(rs.IsBOF()){
throw new AppException("Client view is stale (Phantom
read),
refresh view");
}
// else ok to edit, plus record/ table locked while update prepared
CString sql("UPDATE Client SET Name='");
sql += client->GetName());
sql += " Version = ";
sql += client->GetVersion() + 1;
sql += (" WHERE (ID = ");
sql += client->GetKey();
sql += ')';
mConn->ExecuteSQL(sql);
// release lock
mConn->CommitTrans();
client->IncrVersion();
So given select was successful, will this record be locked due to the
transaction?
How can i lock the appropriate record outside of using CRecordset update etc?
Thanks.