Using Grails 2.3.9 (Groovy (2.2.2), Mysql 5.5.37 (MySQLUTF8InnoDBDialect), JDK 1.7
I'm trying to implement and test the optimistic locking feature from Grails/Hibernate in the controller side.
After my intuition the following
def instance = Group.findByXXX(...) instance.properties = params // ... instance.version = 5 // something smaller than the current instance.save flush:true, failOnError: true
would throw an exception is thrown because of wrong version. However, the instance is saved in any case.
This question is probably the same as this one, just that I don't understand it. This is what I tried after reading the this question/answer:
def copyInstance = copy(instance) // I instantiate a new item, copy all members // from instance to the new one copyInstance = copyInstance.merge() copyInstance.version = 5 // something smaller than the current copyInstance.save flush:true, failOnError: true
This had the expected result (saving failed). But I still don't quite see through it: could someone explain what the difference is between the upper object "instance" and the lower "copyInstance"? And, is this the way the optimistic locking is achieved (it seems to me the extra copying might not be needed)?