Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate + locking

 
Kishore Dandu
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can Hibernate ensure certain level of optimistic or other sort of locking on a entity(i.e entry in a table)??

Or is it that you need to resort to your own ways for this locking??
 
Loren Rosen
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hibernate has built-in support for optimistic locking.
 
Kishore Dandu
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Loren Rosen:
Hibernate has built-in support for optimistic locking.


Can u elaborate and if the object(or internal data) is being locked does hibernate generate a availability exception(status) or something of that sort???
 
Loren Rosen
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The usual way to do this is to have a version property on the object, a <version> tag in the mapping file, and a version column in the table in the database. This is incremented very time the object is updated, and hibernate can check that the version numbers match when you do an update. If they don't it will throw a StaleObjectStateException.

You can also have it do the same thing with a timestamp column.
 
Kishore Dandu
Ranch Hand
Posts: 1934
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Loren Rosen:
The usual way to do this is to have a version property on the object, a <version> tag in the mapping file, and a version column in the table in the database. This is incremented very time the object is updated, and hibernate can check that the version numbers match when you do an update. If they don't it will throw a StaleObjectStateException.

You can also have it do the same thing with a timestamp column.


So, we can define optimistic locking on a class level, during updates??
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kishore Dandu:
So, we can define optimistic locking on a class level, during updates??
Yes, each class can be set up differently if you like. Optimistic locking is also enforced during delete, and clearly it makes no sense for insert.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic