我已经看到其他人遇到过这个问题,但我还没有找到适合我的解决方案。在我的域控制器的更新方法中,我试图通过检查版本和使用拒绝值()来实现乐观锁定;但是,我显然做错了什么。我已经验证正在调用rejectValue(),但它似乎不起作用。
此外,具有不良数据的域实例无论如何都会保存。任何建议或帮助表示赞赏。这是我有问题的更新方法:
def update(Cohort cohortInstance) {
if (cohortInstance == null) {
notFound()
return
}
cohortInstance = Cohort.get(params.id)
if (cohortInstance == null) {
notFound()
return
}
if (params.version) {
def version = params.version.toLong()
if (cohortInstance.version > version) {
cohortInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
[message(code: 'cohort.label', default: 'Cohort')] as Object[],
"Another user has updated this Cohort while you were editing")
render(view: "edit", model: [cohortInstance: cohortInstance])
return
}
}
// Bind browser data to this cohort Instance now that we've passed the concurrency check
cohortInstance.properties = params
if (cohortInstance.hasErrors()) {
respond cohortInstance.errors, view:'edit'
return
}
cohortInstance.save flush:true
request.withFormat {
form multipartForm {
flash.message = message(code: 'default.updated.message', args: [message(code: 'Cohort.label', default: 'Cohort'), cohortInstance.proposedCode])
redirect cohortInstance
}
'*'{ respond cohortInstance, [status: OK] }
}
}