gpt4 book ai didi

每次提交命令对象时,Grails 2.3.7 乐观锁定版本都会更新

转载 作者:行者123 更新时间:2023-12-02 03:22:14 26 4
gpt4 key购买 nike

我有以下内容

def save(ACommand command){
...
}

@Validateable
class ACommand implements Serializable
{
ADomainObject bundleDef
}

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

我还尝试使用两个不同的 session ,没有区别

更新

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

更新2

When you perform updates Hibernate will automatically check the version property against the version column in the database and if they differ will throw a StaleObjectException. This will roll back the transaction if one is active.

per Grails在我看来这应该可行。

最佳答案

据我所知,您需要自己检查版本并处理故障 - 这不会自动发生。您可以使用如下代码来完成此操作:

/**
* 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
}

关于每次提交命令对象时,Grails 2.3.7 乐观锁定版本都会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23249545/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com