See the light - agile, industrial strength, rapid web application development made easy

(Quick Reference) # 6.3.1 Saving and Updating - Reference Documentation Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith, Lari Hotari Version: 2.3.8 ## 6.3.1 Saving and Updating An example of using the save method can be seen below:
    def p = Person.get(1)
    p.save()
This save will be not be pushed to the database immediately - it will be pushed when the next flush occurs. But there are occasions when you want to control when those statements are executed or, in Hibernate terminology, when the session is "flushed". To do so you can use the flush argument to the save method:
    def p = Person.get(1)
    p.save(flush: true)
Note that in this case all pending SQL statements including previous saves, deletes, etc. will be synchronized with the database. This also lets you catch any exceptions, which is typically useful in highly concurrent scenarios involving optimistic locking:
    def p = Person.get(1)
    try {
        p.save(flush: true)
    }
    catch (org.springframework.dao.DataIntegrityViolationException e) {
        // deal with exception
    }
Another thing to bear in mind is that Grails validates a domain instance every time you save it. If that validation fails the domain instance will not be persisted to the database. By default, `save()` will simply return `null` in this case, but if you would prefer it to throw an exception you can use the `failOnError` argument:
    def p = Person.get(1)
    try {
        p.save(failOnError: true)
    }
    catch (ValidationException e) {
        // deal with exception
    }
You can even change the default behaviour with a setting in `Config.groovy`, as described in the section on configuration. Just remember that when you are saving domain instances that have been bound with data provided by the user, the likelihood of validation exceptions is quite high and you won't want those exceptions propagating to the end user. You can find out more about the subtleties of saving data in this article - a must read! | Quick Reference (hide)