-->
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.  [ 6 posts ] 
Author Message
 Post subject: concurrent problem
PostPosted: Fri Oct 28, 2005 4:41 am 
Newbie

Joined: Wed Oct 12, 2005 5:35 am
Posts: 9
hi,
I got a concurrent problem about hibernate3.0
I use <timestamp name="lastDate" column="LAST_DATE"/> as concurrent control
following is my program:

public void save(Object entry)
{
Session session=getSession();
Transaction transaction=session.beginTransaction();
try
{
System.out.println(entry.getLastDate());
session.update(entry);
transaction.commit();
}
catch(Exception e)
{
transaction.rollback();
}
session.close();
}
in normal case, it can work fine, when there is an exception(for example unique checking) occured when "session.update(entry);" the transaction will rollback. after I modify "entry" and call this method to update it into database, I always got a concurrent exception (this object has been updated by other session), but it is impossible because nobody will change this entry
and I found the timestamp column - lastDate has been modified when commit, and do not rollback when the whole transaction been rollback. that's why I always get concurrent exception.
how can I do now? is it a bug of hibernate?

thanks a lot
regards
Simon


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 30, 2005 4:04 am 
Newbie

Joined: Wed Oct 12, 2005 5:35 am
Posts: 9
can anybody give me a hand?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 30, 2005 5:49 am 
Beginner
Beginner

Joined: Thu Jun 02, 2005 5:09 am
Posts: 22
This works as designed: Hibernate will first update your timestamp and then perform the update, but it will not roll back the timestamp property (or any other properties changed during the update, by an Interceptor for example) after an exception occurs during the update. If you want to use the changed object after such an error, don't use optimistic locking or select the object from the database again and merge the changes that have been made into that newly selected object, which will have a timestamp that equals the timestamp of the table row for that object so it will work OK for optimistic locking.
It is strongly discouraged to continue working with a Session after a HibernateException occurs, so use a new Session to do this.

Joris


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 30, 2005 9:25 pm 
Newbie

Joined: Wed Oct 12, 2005 5:35 am
Posts: 9
I see, thanks a lot


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 30, 2005 9:33 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
Simon,
close session in finally block - you don't close session when transaction rollback(maybe)
try like this :

public void save(Object entry)
{
Session session=getSession();
Transaction transaction=session.beginTransaction();
try
{
System.out.println(entry.getLastDate());
session.update(entry);
transaction.commit();
}
catch(Exception e)
{
transaction.rollback();
} finaly {
session.close();
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 31, 2005 5:49 am 
Newbie

Joined: Wed Oct 12, 2005 5:35 am
Posts: 9
dear snpesnpe
thanks for your reply, but the session has been closed outside the try catch. so it is not the point.
jkuipers's answer is correct. I have wrote a interceptor to rollback the version field separately when the transaction rollback. so it is OK now
antway thank you both
regards
Simon


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.