aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate Optimistic Locking Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate Optimistic Locking" Watch "Hibernate Optimistic Locking" New topic
Author

Hibernate Optimistic Locking

Vijay Kashyap
Ranch Hand

Joined: Jul 30, 2001
Posts: 74
Can some body explain if this is the expected behaviour,

My application uses 'version' for optimistic locking and following is used to specify the version property

@hibernate.version column="version_id" type="long"


I am reading a record for update (displayed on GUI). During this update process (before making final update call) some other process (may be external) comes along and updates the same record setting version to a differnet value( +1). I expect Hibernate to throw StaleDataException when the GUI submits the record for updation. But to my surprise the record is updated, which overwrites the last update.

I observsed that an extra select query is fired immediately before update and this select query fetches the latest version which was set by other process. Why Hibernate is making extra select call is something which I have no clue about.


Another observation, When I set Select-Before-Update="true" then only Hibernate complains.


Thanks,
Vijay
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Are you sure that other transaction committed and incremented the record's version field. Because if you have a version column it will be included in the Where clause of the update statement. So

record 1 is

username = "Me" city = "LA, CA" version = 1"

I get it and another user gets this record.

I change username to "You"

other user changes city to "Philadelphia, PA" and updates and commits

in the database the record will now look like

username="Me" city="Philadelphia, PA" version ="2"

I try to update with

Update Table set username="You" WHERE version="1"

Well that update statement should and will fail.

Now, maybe you have a different issue where the version field is not being incremented.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Vijay Kashyap
Ranch Hand

Joined: Jul 30, 2001
Posts: 74
Mark thanks for taking initiative.

version is getting incremented when an update happens, even if I do the update manually by forcing the version update still the next update with old version should fail but thats not happening. Only when I set select-before-update="true" then the update fails.

I am not sure is something else needs to be specified to get the right behaviour.

Regards,
Vijay
Vijay Kashyap
Ranch Hand

Joined: Jul 30, 2001
Posts: 74
Just for information,

I am able to resolve the issue. Its down to the fact that hibernate property hibernate.jdbc.batch_versioned_data is set to true, which causes Hibernate to skip versioning check and hence it is not reporting StaleObjectStateException error and record is not getting updated either.

Thanks
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Originally posted by Vijay Kashyap:
Just for information,

I am able to resolve the issue. Its down to the fact that hibernate property hibernate.jdbc.batch_versioned_data is set to true, which causes Hibernate to skip versioning check and hence it is not reporting StaleObjectStateException error and record is not getting updated either.

Thanks


Ahh, good find. Thanks for posting the resolution.

Mark
Balaji Ramalingam
Greenhorn

Joined: Nov 08, 2004
Posts: 9
Irrespective of the hibernate.jdbc.batch_versioned_data being true or false, I am getting StaleObjectStateException. Any one know about the purpose of hibernate.jdbc.batch_versioned_data flag in hibernate.cfg.xml file?



Balaji R<br />SCJP 1.4,SCBCD 1.3, SCEA(In Progress)
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

From the Hibernate documentation:

Set this property to true if your JDBC driver returns correct row counts from executeBatch() (it is usually safe to turn this option on). Hibernate will then use batched DML for automatically versioned data. Defaults to false.


So if it doesn't appear to be working I'd investigate your Driver.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate Optimistic Locking