Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate Optimistic Locking

 
Vijay Kashyap
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vijay Kashyap
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic