I have an application (ASP.NET MVC) that uses a Next New method to get the next new piece of work. The user clicks on a Next New button, and my NHibernate code would go fetch the next piece of work with an Active status. As the number of users has increased, we have seen an issue arise where 2 users can get the same piece of work. This occurs even though I have wrapped my SELECT and UPDATE calls into a transaction.
I also tried setting the transaction to an IsolationLevel of RepeatableRead, but this would just throw a TransactionException when 2 users try to access the data at the same time.
Here is the process I am currently using:
- Get the next work object
- Update the object to show it is pended and save to the db
- Display the work to the user
Here is the code I am currently using:
using (var txn = _repository.Session.BeginTransaction(IsolationLevel.RepeatableRead))
{
try
{
newAccount = _repository.FindFirstWithLock(criteria, LockMode.Read);
newAccount.QueueStatus = pendingStatus;
_repository.Save(newAccount);
txn.Commit();
}
catch (Exception)
{
txn.Rollback();
throw;
}
}
The criteria is a DetachedCriteria object which just looks for accounts which are active and sorts descending by a date property.
Does anyone have any suggestions on how I could make this work?