dagda1 wrote:
Hi all,
I am trying to avoid concurrency in an ASP.NET using NH 1.2.0. I have come up with this rather crude method:
Code:
public void ValidateRecipe(Recipe recipe)
{
ISession session = _sessionManager.OpenSession(Common.ALIAS);
string hql = "from Recipe r where r.Uid = :uid and r.UpdateTimestamp = :timestamp";
IQuery q = session.CreateQuery(hql);
q.SetParameter("uid", recipe.Uid, NHibernateUtil.Guid);
q.SetParameter("timestamp", recipe.UpdateTimestamp, NHibernateUtil.Timestamp);
Recipe existingRecipe = q.UniqueResult<Recipe>();
if (existingRecipe == null)
throw new ConcurrencyException(string.Format("Recipe {0} has been updated since the last 'get'.", recipe.Uid.ToString()));
_commonDao.SaveOrUpdateObject(recipe);
}
I am simply checking for a the existance of the old record by including the Updatetimestamp in the where clause.
Is there a build in way of doing this in NH or is there as I suspect a better way of doing this?
Cheers
Paul
I think you're not trying to *avoid* concurrency but actually allow it.
I am using Versioning but I use an Int instead of a Timestamp. I think the behavior is the same, but I could be wrong.
In my system, if two users are working with the same object, the first user to save "wins". The second user is given an error message. NHibernate throws StaleObjectStaleException.
My application is WinForms based, but again I would think the behavior is the same. I didn't have to code any extra logic to check for changes, Nhibernate does this automatically because I am using the "version" property.
In my mapping documents, I simply have this:
Code:
<version name="NhVersion" column="nh_version" type="long" unsaved-value="null" />