{"id":442,"date":"2010-03-06T00:02:48","date_gmt":"2010-03-05T18:32:48","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=442"},"modified":"2016-12-19T15:29:23","modified_gmt":"2016-12-19T09:59:23","slug":"grails-handling-staleobjectstateexception-and-making-executeupdate-transactional","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/grails-handling-staleobjectstateexception-and-making-executeupdate-transactional\/","title":{"rendered":"Grails: handling StaleObjectStateException and making executeUpdate transactional"},"content":{"rendered":"<p>Recently I encountered a  StaleObjectStateException which said &#8220;Row was updated or deleted by another transaction.&#8221; We were just updating multiple objects of the same domain inside a loop. So we used executeUpdate statement instead of myClass.save() method to update the records, which worked.<\/p>\n<blockquote>\n<div class=\"code\">\n<pre lang=\"groovy\">ReserveAccount.executeUpdate(\"update ReserveAccount set currentBalance=(:balance) where id=(:id)\", [balance: currBalance, id: reserveAccount.id])\r\n<\/pre>\n<\/div>\n<\/blockquote>\n<p>Later we also needed to update multiple objects of the same domain with same values. But grails executeUpdate statement doesn&#8217;t support list as a parameter. So here on the mailing list I found the solution  <\/p>\n<blockquote>\n<div class=\"code\">\n<pre lang=\"groovy\">AccountTransactionStatus.withSession {session ->\r\n      session.createQuery(\"\"\"update AccountTransactionStatus t set t.thru=(:today) where\r\n                            t.statusFor in (:selectedList)\"\"\")\r\n\t\t\t     .setParameterList('selectedList', transactions)\r\n\t\t\t     .setParameter('today', new Date()).executeUpdate()\r\n    }\r\n<\/pre>\n<\/div>\n<\/blockquote>\n<p>Now the concern was to retain my service as transactional since with executeUpdate statement in there, it couldn&#8217;t be transactional anymore.  So then we used withTransaction on the parent domain as a block to sorround the executeUpdate statement and along with other statements, which worked.<\/p>\n<blockquote>\n<div class=\"code\">\n<pre lang=\"groovy\"> Servicer.withTransaction {\r\n\tmyMap.each {servicerId, amount -&gt;\r\n\t      def servicer = Servicer.read(servicerId)\r\n\t      BigDecimal currBalance = servicer.reserveAccount.currentBalance\r\n\t      currBalance -= amount\r\n\t      ReserveAccount.executeUpdate(\"update ReserveAccount set currentBalance=(:balance) where id=(:id)\", [balance: currBalance, id:\r\n\t\t servicer.reserveAccount.id])\r\n    }\r\n }\r\n<\/pre>\n<\/div>\n<\/blockquote>\n<p>And other solution to handle StaleObjectStateException exception is given here in the mailing list <\/p>\n<p>Hope it helped! <\/p>\n<p>~~Amit Jain~~<br \/>\namit@intelligrape.com<br \/>\nhttp:\/\/www.tothenew.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently I encountered a StaleObjectStateException which said &#8220;Row was updated or deleted by another transaction.&#8221; We were just updating multiple objects of the same domain inside a loop. So we used executeUpdate statement instead of myClass.save() method to update the records, which worked. ReserveAccount.executeUpdate(&#8220;update ReserveAccount set currentBalance=(:balance) where id=(:id)&#8221;, [balance: currBalance, id: reserveAccount.id]) Later we [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":7},"categories":[7],"tags":[181,4840,179],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/442"}],"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\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=442"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/442\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=442"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}