{"id":6894,"date":"2012-09-01T13:14:58","date_gmt":"2012-09-01T07:44:58","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=6894"},"modified":"2016-12-19T15:06:20","modified_gmt":"2016-12-19T09:36:20","slug":"6894","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/6894\/","title":{"rendered":"Optimistic locking strategy in Grails"},"content":{"rendered":"<p>In Grails optimistic locking is achieved by version property . The Grails Domain classes has a built in property called &#8220;version&#8221;. This property can be used for optimistic locking. Although you can remove this property<br \/>\n[java]<br \/>\nstatic mapping ={<br \/>\n      version false<br \/>\n}<br \/>\n[\/java]<br \/>\nbut its not a very good practice. The initial value of version field is 0 and Grails automatically increments it by 1 every time the object is updated. This property helps to identify whether the currently updating object has already been updated or not. <\/p>\n<p>&nbsp;<\/p>\n<p>Suppose we have a Book domain and its corresponding BookController the default Grails scaffold update action has code something like as follows<br \/>\n[java]<br \/>\nif (version != null) {<br \/>\n            if (book.version &gt; version) {<br \/>\n                book.errors.rejectValue(&quot;version&quot;, &quot;default.optimistic.locking.failure&quot;,<br \/>\n                        [message(code: &#8216;book.label&#8217;, default: &#8216;Book&#8217;)] as Object[],<br \/>\n                        &quot;Another user has updated this Book while you were editing&quot;)<br \/>\n                render(view: &quot;edit&quot;, model: [book: book])<br \/>\n                return<br \/>\n            }<br \/>\n        }<br \/>\n[\/java]<br \/>\nAs we can see it checks the current version of the object with the version got from request. If the current version of object is greater, it injects the error into the object and object is not saved.<\/p>\n<p>&nbsp;<\/p>\n<p>Hope it helps<br \/>\n<a href=\"http:\/\/www.tothenew.com\/blog\/author\/uday\/\">Uday Pratap Singh<\/a><br \/>\n<a href=\"mailto:uday@intelligrape.com\">uday@intelligrape.com<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Grails optimistic locking is achieved by version property . The Grails Domain classes has a built in property called &#8220;version&#8221;. This property can be used for optimistic locking. Although you can remove this property [java] static mapping ={ version false } [\/java] but its not a very good practice. The initial value of version [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[7],"tags":[941,913,917,940,914],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6894"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=6894"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6894\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=6894"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=6894"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=6894"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}