1
votes

I have been saving entities in my database in grails with the following shorthand...

def jim = new User(name: "Jim",
                emailAddress: "[email protected]",
                backOfficeUser: false,
                dateCreated: Instant.now(),
                lastUpdated: Instant.now(),
                passwordHash: BCrypt.hashpw("secret123", BCrypt.gensalt())).save(flush: true)

It all seemed to be working fine until something caused .save() and save(flush: true) are returning null. If I change the statement to this it works fine however...

    def jim = new User(name: "Jim",
            emailAddress: "[email protected]",
            backOfficeUser: false,
            dateCreated: Instant.now(),
            lastUpdated: Instant.now(),
            passwordHash: BCrypt.hashpw("secret123", BCrypt.gensalt()))
            jim.save(flush:true)

The Jim instance would then immediately have an id issued by the database sequence and is persisted when save is called.

2
Try adding failOnError: true in save for the first case to see if there is any validation error. - dmahapatro

2 Answers

5
votes

In grails save returns saved instance if save was successfull, if not - it'll return null. See save reference description section. Add failOnError: true to check if validation failed

1
votes

It turns out that another developer in the team had overridden the save method to retry when we hit a specific deadlock exception our database vendor can have under high load (Percona).

In this new save method we no longer returned the instance.