{"id":6983,"date":"2012-09-03T20:49:46","date_gmt":"2012-09-03T15:19:46","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=6983"},"modified":"2016-12-19T15:06:21","modified_gmt":"2016-12-19T09:36:21","slug":"pessimistic-locking-in-grails","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/pessimistic-locking-in-grails\/","title":{"rendered":"Pessimistic locking in Grails"},"content":{"rendered":"<p>We already know that by default Grails scaffold comes with Optimistic locking and it is achieved by version field. Now lets see how Pessimistic locking is achieved in Grails.<br \/>\nGrails has a built in method to acquire lock on object. To acquire a lock we will do something like following<br \/>\n[java]<br \/>\nBook book = Book.get(1)<br \/>\nbook.lock()<br \/>\n[\/java]<br \/>\nThe above code will fire 2 queries one for getting the object and other for taking the lock on it, something like as follows<br \/>\n[java]<br \/>\nHibernate: select book0_.id as id1_0_, book0_.version as version1_0_, book0_.name as name1_0_, book0_.price as price1_0_ from book book0_ where book0_.id=?<br \/>\nHibernate: select id from book where id =? and version =? for update<br \/>\n[\/java]<br \/>\nor we can do both the tasks in single go by using the static lock method<br \/>\n[java]<br \/>\nBook book = Book.lock(1)<br \/>\n[\/java]<br \/>\nThe above code will fire only one query<br \/>\n[java]<br \/>\nHibernate: select book0_.id as id1_0_, book0_.version as version1_0_, book0_.name as name1_0_, book0_.price as price1_0_ from book book0_ where book0_.id=? for update<br \/>\n[\/java]<\/p>\n<p>Whenever there is simultaneous updations of an object occurs it throws StaleObjectException.   <\/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>We already know that by default Grails scaffold comes with Optimistic locking and it is achieved by version field. Now lets see how Pessimistic locking is achieved in Grails. Grails has a built in method to acquire lock on object. To acquire a lock we will do something like following [java] Book book = Book.get(1) [&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":15},"categories":[7],"tags":[938,4840,939,915,926,927],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6983"}],"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=6983"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6983\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=6983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=6983"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=6983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}