GORM: a silent failure …

I was pulling my hair out yesterday trying to determine why my application wasn’t saving all of it’s data to the database. There were no exception and only some of the data was getting written. I tested things again and again and I couldn’t see why the data wasn’t being saved.

I posted to the Nabble grails forum where I knew I would get a response quickly and it was suggested by Mike Brevoort that I try checking for errors from the save method. Of course this suggestion hit me like a brick since I should have known better than to ignore the return codes etc. I thought I was safe not worrying about this as I expected and exception to be thrown by someone if the data or relationships were invalid.

This is the approach suggested by Mike and I suggest you use it too, since a silent failure will take up a lot of your time:

if (!record.save(flush: true)) {
    println "GORM problems saving record $record"
    record.errors.each {
        println "$it"
    }
}

I could have saved myself a lot of time by checking the return codes / method result, but I’m also a little annoyed at the silent failure by GORM and Hibernate !

I hope this tip saves you a lot of stress and time.

Thanks Mike !

Leave a Reply

You must be logged in to post a comment.