Grails: handling StaleObjectStateException and making executeUpdate transactional « Intelligrape Groovy & Grails Blogs

Grails: handling StaleObjectStateException and making executeUpdate transactional

Posted by

Recently I encountered a StaleObjectStateException which said “Row was updated or deleted by another transaction.” 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("update ReserveAccount set currentBalance=(:balance) where id=(:id)", [balance: currBalance, id: reserveAccount.id])

Later we also needed to update multiple objects of the same domain with same values. But grails executeUpdate statement doesn’t support list as a parameter. So here on the mailing list I found the solution

AccountTransactionStatus.withSession {session ->
      session.createQuery("""update AccountTransactionStatus t set t.thru=(:today) where
                            t.statusFor in (:selectedList)""")
			     .setParameterList('selectedList', transactions)
			     .setParameter('today', new Date()).executeUpdate()
    }

Now the concern was to retain my service as transactional since with executeUpdate statement in there, it couldn’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.

 Servicer.withTransaction {
	myMap.each {servicerId, amount ->
	      def servicer = Servicer.read(servicerId)
	      BigDecimal currBalance = servicer.reserveAccount.currentBalance
	      currBalance -= amount
	      ReserveAccount.executeUpdate("update ReserveAccount set currentBalance=(:balance) where id=(:id)", [balance: currBalance, id:
		 servicer.reserveAccount.id])
    }
 }

And other solution to handle StaleObjectStateException exception is given here in the mailing list

Hope it helped!

~~Amit Jain~~
amit@intelligrape.com

http://www.intelligrape.com

This entry was posted on March 6th, 2010 at 12:02 am and is filed under Grails . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

3 Responses to “Grails: handling StaleObjectStateException and making executeUpdate transactional”

  1. I was pleased to discover this web-site.I want to to thanks for your time for this great read!! I definitely enjoying every single bit of it and i have you saved as a favorite to look into new stuff you blog post.

  2. Trackback
    [...] I am so happy I found this web site, found by error, while I was looking on Askjeeve for [...]

  3. LOOK AT THIS says:

    Hey there this is kinda of off topic but I was wanting to know if
    blogs use WYSIWYG editors or if you have to manually code with HTML.

    I’m starting a blog soon but have no coding skills so I wanted to get guidance from someone with experience. Any help would be greatly appreciated!

Leave a Reply