Alex Rivera | Logout

Technical reasons not to set grails.gorm.autoFlush and grails.gorm.failOnError to true in Config.groovy?

Asked 2011-01-15T22:32:55.950
12

Besides the obvious performance implications of doing so, what are good technical reasons not to set grails.gorm.autoFlush = true and grails.gorm.failOnError = true in Config.groovy?

Edit
Report

1 Answer

17

GORM is a wrapper around Hibernate (at least that's one of the implementations - there are now also various NoSQL implementations like Redis). By setting autoFlush to true you're denying Hibernate the opportunity to optimise the calls it makes to the database. For example, you may cause it to insert then later update, when a single insert might have been sufficient. There's nothing inherently bad about that, it's just not necessary and is less efficient. Hibernate is clever enough to know when it needs to write to the database and can optimise - you've abstracted away that problem.

Setting failOnError would cause save to throw an exception whenever you try to save a domain object that doesn't validate. When building an application which involves creating objects from user input it's pretty normal for objects not to validate - missing inputs, wrong formats, etc. Exception handling should be saved for exceptions and errors - it shouldn't be used for normal flow of an application. save() returns the object when the object was successfully saved or null otherwise, which gives you a more convenient way to handle validation as part of the application flow, rather than putting try-catch blocks all over the place.

Peter Ledbrook (on of the authors of Grails in Action) has written a great series of 'GORM Gotchas' articles in which he discusses some of these issues in more detail - well worth a read: part 1, part 2 & part 3.

answered 2011-01-15T23:09:00.380

Your Answer