GORM: default behaviour is not to throw Exception when save() fails

Symptoms:

I was using Grails 1.1.1 and could not understand why my application was not throwing an Exception when I was inserting a record that clearly was violating some constraints (like unique indexes). I checked to see if the record was created, and it wasn’t, as expected. So, the problem was that GORM was not throwing an Exception when something goes wrong with the save() method call.

Solution:

I found out that not throwing an Exception in save() is the default behavior in GORM. But there is a parameter you can set to true on the save method called “failOnError”.

myDomainObject.save(failOnError:true)

But to make this work I needed to upgrade my Grails to the latest version, currently 1.2.2. Then it worked!

You can also change this default behavior putting the following setting in Config.groovy:

grails.gorm.failOnError=true

Thanks to:

http://www.brentbaxter.com/2010/03/10/grails-tip-using-failonerror-when-saving-domain-instances/

http://jira.codehaus.org/browse/GRAILS-4984

Posted in programming | Tagged , , | Leave a comment