Grails: What makes checking and updating atomic version?

An action updatein Grails first checks the version of the updated object and then updates it. What part of Grails ensures that the object is not updated by another request while checking the version and updating the object?

Update:

Yes, hibernate will check the version, when it smoothes an object and throws an exception, optimization of the lock is not performed. And I think hibernate will verify that validation + update is atomic, but ...

if you look at the method of generating the generated Grails, you will find that the first double checks of grails and then (from my point of view) are not ready to handle the exception. The probability that hibernate will throw an exception after the update method has already checked the correct version is small, but it seems to me that this is possible.

So wouldn’t it be enough to try to save and catch the exception (if any)?

+3
source share
1 answer

It is controlled by the Hibernate layer. It is called “optimistic locking,” and basically it only updates an object with a known version. For instance:

UPDATE SET
  %... fields ...%, 
  version = version + 1                      --update version to a new value
WHERE 
  id = %obj id%                              --current object
  AND version = %previous obj version%`      --last known version 

And throw an exception if it is not updated (by the way, at the moment it is difficult to repair this error, in most cases you just lose your update).

, , ( /):

try {
    if (!obj.save(flush: true)) {
        // validation error
    }
} catch (OptimisticLockingFailureException e) {
    // not saved
}

. , .

MyDomain obj = MyDomain.lock(params.id) //now it locked for update
// update fields
obj.save()

GORM http://grails.org/doc/latest/guide/GORM.html#locking

+3

All Articles