I have a SQL Server (2012) which I access using Entity Framework (4.1). In the database I have a table called URL into which an independent process feeds new URLs. An entry in the URL table can be in state "New", "In Process" or "Processed".
I need to access the URL table from different computers, check for URL entries with status "New", take the first one and mark it as "In Process".
var newUrl = dbEntity.URLs.FirstOrDefault(url => url.StatusID == (int) URLStatus.New);
if(newUrl != null)
{
newUrl.StatusID = (int) URLStatus.InProcess;
dbEntity.SaveChanges();
}
//Process the URL
Since the query and update are not atomic, I can have two different computers read and update the same URL entry in the database.
Is there a way to make the select-then-update sequence atomic to avoid such clashes?