开发者

Disabling locking for non-critical Grails/GORM domain classes

开发者 https://www.devze.com 2022-12-11 12:43 出处:网络
Assume the following code in a Grails controller: def action = { ClassName o = ClassName.findByFoo(params.foo)

Assume the following code in a Grails controller:

def action = {
  ClassName o = ClassName.findByFoo(params.foo)
  if (o) {
    o.counter += 1
  }
}

By default Grails uses optimistic locking via the version column added by default to all GORM database tables. However, if a sufficiently large number of multiple concurrent requests are sent to this action the optimistic locking mechanism will break down with the following exception:

org.hibernate.StaleObjectStateException:
  Row was updated or deleted by another transaction (or unsaved-value mapping was 
  incorrect): [ClassName#id]

For domain objects where a failed update/delete开发者_如何学编程 is totally non-critical I'd like to disable the locking mechanism, so that no StaleObjectStateException will be thrown thrown. How do I achieve that?


From mapping DSL docs: you can disable it thusly:

class Person {
  ..
  static mapping = {
      table 'people'
      version false
  }
}

I doubt you can disable it for a specific call.

0

精彩评论

暂无评论...
验证码 换一张
取 消