gpt4 book ai didi

spring - 错误: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)

转载 作者:行者123 更新时间:2023-12-02 20:46:07 32 4
gpt4 key购买 nike

我有以下操作:

def index() {

User.withNewTransaction {

def user = User.get(params.userId)
user.name = "test"
user.save(flush:true)

response.setContentType("image/gif")
response.outputStream << PIXEL_BYTES_OF_A_GIF_IMAGE
return
}
}

运行时,有时会出现以下错误:

Message
Executing action [index] of controller [test.TestController] caused exception: Runtime error executing action
Caused by
Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [test.User#1]

为什么会出现这个错误?我认为 withNewTransaction 可以防止此错误。

最佳答案

您可以使用pessimistic locking

用途:

User user = User.lock(params.userId)

User user = User.findById(params.userId, [lock: true])    

关于spring - 错误: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18110765/

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