Monday, 23 June 2008

Pessimistic Locking in Grails

I blogged about this a few weeks ago, then decided I really ought to create a few test cases to back up what I was spouting and raise a JIRA. Finally done. Here it is.

GRAILS-3156

Good news is the grails guys have scheduled a fix for v1.0.4

For anyone with an aversion to clicking links the long and short of it is that you can't pessimistically lock anything with the grails API. Instead you have to fall back to using hibernate and even then the only reliable way seems to be to clear the session and perform either a session.get(DomainObj.class, id, LockMode.UPGRADE) or session.load(DomainObj.class, id, LockMode.UPGRADE).

If you're not keen on clearing the entire session, you could try evicting the object you're about to lock instead, but this can cause other problems. Failing that you could write some db dependent SQL like 'LOCK <table> IN ACCESS EXCLUSIVE MODE'. Just make sure you check the db docs and pick the least restrictive lock that will do the job.

1 comment:

hollywood said...

On a tangent, but also linked to issues with Hibernate flushing, specifically around dynamic binding of properties

http://www.cacoethes.co.uk/blog/index.php?p=38&c=1