以下是我在 api 调用上运行的代码。下面的代码在 grails 服务中,默认情况下是事务性的。但即使在锁定行之后,我也会收到这个错误:Message: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)
。数据库中只有一个计划,所以 for 循环只运行一次。但是对 api 的并发调用会导致错误。请帮我解决这个问题
def updateAllPlans(def user,def percentComplete,def chapterId){
def plans = Plan.findAllWhere(user:user)
def chapter = Chapter.findById(chapterId)
for(def plan:plans){
def foundChapters = plan.psubject.ptopics.psubtopics.pchapters.chapter.flatten().contains(chapter)
if(foundChapters){
plan.lock()
if(percentComplete=='0'){
plan.durationViewComplete = plan.durationViewComplete.plusMillis(chapter.duration.getMillisOfSecond())
plan.durationViewComplete = plan.durationViewComplete.plusSeconds(chapter.duration.getSecondOfMinute())
plan.durationViewComplete = plan.durationViewComplete.plusMinutes(chapter.duration.getMinuteOfHour())
plan.durationViewComplete = plan.durationViewComplete.plusHours(chapter.duration.getHourOfDay())
}else{
plan.durationComplete = plan.durationComplete.plusMillis(chapter.duration.getMillisOfSecond())
plan.durationComplete = plan.durationComplete.plusSeconds(chapter.duration.getSecondOfMinute())
plan.durationComplete = plan.durationComplete.plusMinutes(chapter.duration.getMinuteOfHour())
plan.durationComplete = plan.durationComplete.plusHours(chapter.duration.getHourOfDay())
}
plan.save(flush:true, failOnError:true)
}
}
}