【问题标题】:Error: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)错误:行已被另一个事务更新或删除(或未保存值映射不正确)
【发布时间】:2013-08-09 06:21:49
【问题描述】:

我有以下动作:

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 会阻止这个错误。

【问题讨论】:

标签: spring hibernate grails transactions grails-2.0


【解决方案1】:

您可以使用pessimistic locking

用途:

User user = User.lock(params.userId)

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-02
    • 2013-05-17
    • 1970-01-01
    • 1970-01-01
    • 2019-06-01
    • 2021-11-20
    • 2014-11-22
    • 2013-09-07
    相关资源
    最近更新 更多