Grails 2.3.7. Optimistic lock is updated every time a Command object is sent

I have the following

def save(ACommand command){
  ...
}

@Validateable
class ACommand implements Serializable
{
  ADomainObject bundleDef
}

but every time the name is saved, the version is incremented. Therefore, if I open two browsers and send another value sequentially, instead of receiving the error a second time, as expected, the value is updated.

I also tried using two different sessions without a difference

Update

If I use breakpoints and submit before the other is complete, it works fine. However, if I allow the first full, then send the second without updating, the version will be updated to a newer one (which I don’t want), and the change will pass.

Update 2

, Hibernate , , StaleObjectException. , .

per Grails, , .

+4
2

AFAIK, - . :

/**
 * Returns a boolean indicating whether the object is stale
 */
protected boolean isStale(persistedObj, versionParam = params.version) {

    if (versionParam) {
        def version = versionParam.toLong()
        if (persistedObj.version > version) {
            return true
        }
    } else {
        log.warn "No version param found for ${persistedObj.getClass()}"
    }
    false
}

isStale

def update() {
    Competition competition = Competition.get(params.id)

    if (isStale(competition)) {
        // handle stale object
        return
    }

    // object is not stale, so update it
}
0

, , , , , save().

, Hibernate - . save(), Hibernate, . , Hibernate.

- , , , , , . , , , , , , . : , , - . , , .

, .

0

Source: https://habr.com/ru/post/1537743/


All Articles