Grails 2.3.7 Optimistic Locking 版本在每次提交命令对象时更新

Jac*_*kie 5 grails grails-orm optimistic-locking

我有以下

def save(ACommand command){
  ...
}

@Validateable
class ACommand implements Serializable
{
  ADomainObject bundleDef
}
Run Code Online (Sandbox Code Playgroud)

但每次 save 被调用时,版本都会增加。因此,如果我打开两个浏览器并连续提交不同的值,而不是像我预期的那样第二次出错,而是更新了值。

我也尝试使用两个不同的会话没有区别

更新

如果我使用断点并在另一个断点完成之前提交它工作正常。但是,如果我让第一个完成,然后在不刷新的情况下提交第二个,则版本将更新为较新的(我不想要的)并且更改会通过。

更新 2

当您执行更新时,Hibernate 将自动根据数据库中的 version 列检查 version 属性,如果它们不同,将抛出 StaleObjectException。如果事务处于活动状态,这将回滚事务。

每个 Grails这在我看来应该有效。

Dón*_*nal 0

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
}
Run Code Online (Sandbox Code Playgroud)

isStale你可以从这样的动作中调用

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

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

    // object is not stale, so update it
}
Run Code Online (Sandbox Code Playgroud)