如何用JPA和Hibernate修复StaleObjectStateException [英] How to fix StaleObjectStateException with JPA and Hibernate
问题描述
def updateObject(){
Object o = Object.get params.id as Long)
o.otherObjects.clear()
objectDataService.saveObject(o.id)
OtherObject newObject = new OtherObject ;
o.addToOtherObjects(newObject)
objectDataService.saveObject(o.id)
}
ServiceLogic
def saveObject(long profileId){
o.save(flush:true)
}
在90%的情况下,这只会发生作用。 b 问题 我已阅读相关问题,并找到了上面看到的
$ b
错误错误。 GrailsExceptionResolver - 处理请求时发生StaleObjectStateException:[GET] / controller / updateObject - 参数:
stuff []:data
行由另一个事务更新或删除(或未保存值映射不正确):[ com.path.Object#1]。
Stacktrace如下:
消息:行被另一个事务更新或删除(或未保存值映射不正确):[com.path.Object#1]
merge
调用。它解决了大约50%的情况,但不是全部。
解决方案
对于我们来说,几种不同的方法终于解决了StaleObjectException的定期出现问题:
object = object.refresh()
在取回对象后刷新对象解决了我们大部分的StaleObjectExceptions。特别是在某些人有可能从别处工作过同一个对象并改变了其中一些成员的情况下(大部分问题都是由集合成员提供的)。
总体项目稳定性:
错误链接的资源
我们在特定的资源文件上有一个404实际上并不需要,因此在一段时间内忽略它。事实证明,缺少的文件会导致会话保持打开状态 - 从而左右制作StaleObjects。因此,作为向任何面对通常(某些StaleObjects可能总是出现 - 请参阅上面的答案)StaleObjectExceptions:确保所有资源链接正确,并且您的开发人员工具(Chrome F12)不会报告任何丢失的文件。 p>
Controller Logic:
def updateObject() {
Object o = Object.get(params.id as Long)
o.otherObjects.clear()
objectDataService.saveObject(o.id)
OtherObject newObject = new OtherObject;
o.addToOtherObjects(newObject)
objectDataService.saveObject(o.id)
}
ServiceLogic
def saveObject(long profileId) {
o.save(flush:true)
}
what happens
in 90% of the cases this will just work.
problems
ERROR errors.GrailsExceptionResolver - StaleObjectStateException occurred when processing request: [GET] /controller/updateObject - parameters:
stuff[]: data
Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [com.path.Object#1].
Stacktrace follows:
Message: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [com.path.Object#1]
i have read through related questions and found the merge
call you see above. it solved about 50% of the cases but not all.
For us a few different approaches finally solved the StaleObjectException from occuring regularly:
object = object.refresh()
refreshing objects after retrieving them solved most of our StaleObjectExceptions. Especially in cases where there was a possibility that someone would have worked on the same object from somewhere else and changed some of its members (most problems arrived with collection members).
Overall Project stability:
wrongly linked resources
we had a 404 on a specific resource file we did not actually need and therefore ignored it for some time. turns out that the missing file would cause the session to be kept open - thus fabricating StaleObjects left and right.
Therefore as a hint to anyone facing more than the usual (some StaleObjects might always occur - see above answers) StaleObjectExceptions: Make sure that all resources are linked correctly and your developer tools (Chrome F12) do not report any missing files.
这篇关于如何用JPA和Hibernate修复StaleObjectStateException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!