The update action in Grails first checks for the version of the object to be updated and then updates it.
What part of Grails ensures that the object isn’t updated by another request during checking the version and updating the object?
Update:
Yes, hibernate will check for the version when savin the object and will throw an exception is optimistic locking fails. And I guess hibernate will make sure that the check+update is atomic, but…
if you take a look at the grails generated update method, you’ll find that grails first double-checks and then (from my point of view) isn’t prepared to handle the exception. The chances that hibernate will throw an exception after the update method has already checked for the right version are small, but it seems possible to me.
So wouldn’t it be enough to try a save and catch the exception (if there is one)?
It’s managed by Hibernate layer. It’s called ‘optimistic locking’, and basically it updates only a object with a known version. Like:
And throw exception when it fails to update (btw, at this moment it’s hard to recover from this error, at most cases you just losing your update).
If you want to be sure that data is saved, try to force data saving (and check for saving/validtion error):
or even lock data before update. It’s useful only when you have a lot of concurrent updates.
See more details about GORM locking at http://grails.org/doc/latest/guide/GORM.html#locking