gpt4 book ai didi

hibernate - 使用动态查找器( hibernate )使StaleObjectException失效

转载 作者:行者123 更新时间:2023-12-02 14:43:17 25 4
gpt4 key购买 nike

我有一个批处理作业,该作业始终在动态查找器上引发陈旧的对象异常。理想情况下,我不会为此工作运行ORM解决方案,但是我别无选择。异常(exception)发生在FormulaTagService中,这是从FormulaBatchPopulatorService调用的。该应用程序正在使用一个数据库的两台服务器上运行。一台服务器仅执行批处理。
我的问题是:
a)为什么简单的select语句导致域对象实例(在给定事务期间未对对象进行任何更改)最终会保留在 session 中,从而导致陈旧的对象异常?
b)是否可以在交易结束时持久保存对Formula.tags进行的排序,从而在其他人在另一台服务器上修改公式时导致staleobjectexception?
注意,我确实将服务更改为只读,但仍然收到陈旧的对象异常。任何帮助将不胜感激。
配方标签服务

@Cacheable("formulaJob")
def getFormulaByTeacherTagsOrDefaultBatchJob(Long evaluationTemplateId, List teacherTags) {
Long formulaByTagsId = existsFormulaWithSameTagsBatchJob(evaluationTemplateId, teacherTags)

if (DefaultFormulaForEvaluationTemplate.get(evaluationTemplateId) == null && formulaByTagsId ==
null) {
return null;
}

Long defaultFormulaId = DefaultFormulaForEvaluationTemplate.get(evaluationTemplateId).formulaId
return formulaByTagsId ?: defaultFormulaId
}

def existsFormulaWithSameTagsBatchJob(Long evaluationTemplateId, List tags){
// LINE BELOW THROWING STALE OBJECT EXCEPTIONS
def formulas = Formula.findAllByExtEvaluationTemplateIdAndIsActive(evaluationTemplateId, true)

for (Formula formula: formulas) {
def formulaTags = formula.tags
if (existsTagMatchIgnoringBlankTags(tags, formulaTags)) {
def id = formula.id
formula.discard()
return id
}
}
}

@CacheEvict(value='formulaJob', allEntries=true)
def resetTags(){
}

def existsTagMatchIgnoringBlankTags(List tagsToCompare, List tagsExisting) {
if (!tagsToCompare || !tagsExisting) {
return false
}
else {
return tagsToCompare?.sort() == tagsExisting?.sort()
}
}
FormulaBatchPopulatorService代码段
   //Doing this below to improve performance of batch processing
if(index%250==0){
cleanUpGorm()
formulaTagService.resetTags() //cache-evict in formulatagservice
}

def cleanUpGorm(){
def session = sessionFactory.currentSession
session.flush()
session.clear()
propertyInstanceMap.get().clear()
}

最佳答案

我相信您的回答是正确的:

b) Is it possible that the sorting being done on formula.tags is being persisted at the end of the transaction, thus causing a staleobjectexception if someone else is modifying the formula on a different server?



如果您对标签进行排序并且它是一个列表,我相信Groovy会在适当的位置执行此操作,即对原始列表进行排序并返回它。然后,该列表将在以下时间之一保留:
  • 交易结束
  • 请求结束
  • 下次在当前 session 中执行数据库查询时(例如,findBy将刷新 session )

    由于index字段可能已更改,因此它一直存在,因此GORM / hibernate认为它很脏。

    我有一个类似的问题,导致了同样的问题。我在验证它时遇到了这个问题,看看是否有人遇到过它。

    不确定只读部分是怎么回事!您的服务具有交易性吗?

  • 关于hibernate - 使用动态查找器( hibernate )使StaleObjectException失效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24004083/

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