-->
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.  [ 1 post ] 
Author Message
 Post subject: Select a record before updating it to the database
PostPosted: Tue May 22, 2012 10:55 am 
Newbie

Joined: Tue May 22, 2012 10:37 am
Posts: 1
The issue is simple in that there are more than one thread which tries to update a record by doing a select before an update for a record.
But what happens is that in this case there are two records updated with the same city. If city is made as a unique constraint then the other thread gets an unique constraint violation exception error. So the other thread needs to handle this exception and re process the request.

So how do we make the below piece of code work at high concurrency situations without having a unique constraint kind of approach. This would be reactive
i.e. wait for an exception to occur and handle the same. Please share what best practices could be used here to make this work at concurrent loads.

public MyHibernateObject insertOrUpdate(MyHibernateObject newObj, Session s) {
String name = newObj.getCityName();
MyHibernateObject existingObj = getByCityName(name, s);
if ( existingObj == null ) {
s.saveOrUpdate(newObj);
return newObj;
} else {
existing.copyImportantFields(newObj);
s.saveOrUpdate(existing);
return existing;
}
}


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

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.