-->
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: Optimistic locking
PostPosted: Thu Oct 22, 2009 1:25 am 
Newbie

Joined: Thu Sep 10, 2009 3:03 pm
Posts: 5
I have a model object Person that I persist with hibernate.
I have a field versionNumber that has a @Version tag on it and I have a setter and getter (setVersionNumber and getVersionNumber). Optimistic concurrency control works fine and I can see that my version is incrementing properly and that concurrent changes are caught correctly.


I was expecting the following to fail:
int id = 1;
Person p = personDao.load(id);
p.setVersionNumber(p.getVersionNumber() - 1); // fake a change to an old version
p.setName("new name");
personDao.update(p); <--- I would expect this call to fail because I'm using an old version.

Is this related to the fact that my object is attached ?

Any thoughts?


Top
 Profile  
 
 Post subject: Re: Optimistic locking
PostPosted: Thu Oct 22, 2009 3:46 am 
Newbie

Joined: Fri Mar 20, 2009 4:37 am
Posts: 6
I reckon it works because there's only process updating a record.

Try this
Code:
Person one = personDao.load(id);
Person two = personDao.load(id);

one.setName("new name");
two.setName("second new name");

// this will work
personDao.save(one);

// this should fail
personDao.save(two);


Person one update will change the version no. Thus, person two update will fail as its version no differs from the one in the database.

I think version should be read only property.

Hope it helps


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.