【发布时间】:2014-06-05 04:00:33
【问题描述】:
使用 Grails 2.3.9(Groovy (2.2.2)、Mysql 5.5.37 (MySQLUTF8InnoDBDialect)、JDK 1.7
我正在尝试在控制器端实现和测试 Grails/Hibernate 的乐观锁定功能。
我的直觉如下
def instance = Group.findByXXX(...)
instance.properties = params
// ...
instance.version = 5 // something smaller than the current
instance.save flush:true, failOnError: true
会因为版本错误而抛出异常。但是,无论如何都会保存实例。
这个问题大概和this one一样,只是没看懂。这是我在阅读此问题/答案后尝试的:
def copyInstance = copy(instance) // I instantiate a new item, copy all members
// from instance to the new one
copyInstance = copyInstance.merge()
copyInstance.version = 5 // something smaller than the current
copyInstance.save flush:true, failOnError: true
这有预期的结果(保存失败)。但我还是不太明白:有人可以解释上层对象“instance”和下层“copyInstance”之间的区别吗?而且,这是实现乐观锁定的方式吗(在我看来,可能不需要额外的复制)?
【问题讨论】: