can I queue the update operation if an update is is already being performed on the object,to avoid database stale state exceptions. Is there a way to do it in grails ?
Yes, by using explicit locking. By default optimistic locking is used and this works in many scenarios where you don't expect concurrent edits, or you expect them to be unlikely. But if you do expect concurrent edits, then you should let the database do the work for you.
In your controller or other code, instead of doing something like
def thing = Thing.get(params.id)
thing.properties = params
thing.save()
move the code to a transactional service method. Services are transactional by default, so there's no configuration required. Use the lock method there, since it has to be done in a transaction, and database updates should be done in a transaction anyway:
class ThingService {
void updateThing(params) {
def thing = Thing.lock(params.id)
thing.properties = params
thing.save()
}
}
My preference would be to not simply pass the params
map, but instead use individual names properties, but that's not relevant to this issue.