1

I am new to Grails and Goovy. I have reviewed the Grails Framework docs and the discussion about Optimistic vs Pessimistic locking here

Its clear from the docs that:

  • Grails uses optimistic locking by default.

  • Optimistic locking can be turned off (by using version=false)

  • If you turn off Optimistic locking and care about loosing data when concurrent updates occur then you must enable Pessimistic locking (by using the lock() method)

My questions:

  • It seems there are 3 modes. Two explicitly named: Optimistic and Pessimistic, and the other implied - where you have overridden Optimistic but not enabled Pessimistic. Is this correct?

  • If correct - what is this "middle" state called and are there any legitimate uses of this? (The reason for asking is I am reviewing someones code and there is a lot of use of this "middle" state and they are not around to ask, have not documented it etc.... ).

Community
  • 1
  • 1
user2195559
  • 311
  • 1
  • 11

2 Answers2

1

I think if you turn off versioning, you're basically turning off lock checking. So the third state would be basically no-versioning => all writes go through, no conflicts are detected.

From the Hibernate chapter of 'Programming Grails' -

Grails defaults to enabling optimistic locking for all domain classes and in general this shouldn’t be changed. But if you need to, for example when mapping to a legacy database or for tables that aren’t updated and therefore have no risk of concurrent edits, you can disable it with version false.

Tomas Lin
  • 3,532
  • 1
  • 22
  • 19
1

"Optimistic" and "pessimistic" are not necessarily global modes. You can access one object in one way and another object in another way.

For a start, this is a very good and recent (Nov. 2012) article by Marc Palmer: The false optimism of GORM and Hibernate. Its main conclusion is that, well, it's complicated, and that Grails doesn't really handle concurrency well out of the box. That you really can lose data or get weird exceptions in random places unless you really understand how Hibernate works on top of the database, and how Grails works on top of Hibernate.

Sergey Orshanskiy
  • 6,794
  • 1
  • 46
  • 50