-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Concurrency
PostPosted: Thu Feb 15, 2007 11:10 am 
Regular
Regular

Joined: Fri May 05, 2006 11:54 am
Posts: 51
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


Top
 Profile  
 
 Post subject: Re: Concurrency
PostPosted: Thu Feb 15, 2007 4:39 pm 
Regular
Regular

Joined: Fri Jan 27, 2006 2:32 pm
Posts: 102
Location: California, USA
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" />


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.