【发布时间】:2016-12-14 18:48:18
【问题描述】:
(我将在示例中使用 Groovy/Grails 语法)
在休眠时加载实体时,它们会保存在会话缓存/L1 中,问题是如果在会话之外更改它们不会被引用,即使我通过 GORM 方法重新查询它们。这就是我使用 refresh() 的原因。
(以下所有操作都在一个会话中完成。)
User.withNewTransaction {
User user = User.findById(1L, [lock: true]) //select for update
user.name='new name'
user.save()
}
//user entity gets updated on a different thread.
User.withNewTransaction {
user = User.findById(1L, [lock: true]) //another select for update
//at this point, the user entity is not yet filled in with the updated values from the different thread
//so I'm forced to do a refresh so that the user will have the correct values
user.refresh()
//update user
}
是否有替代方案?
这意味着我必须查询两次以确保我们在第二笔交易中获得正确的值。
【问题讨论】: