0
votes

I have run into a baffling situation in a Grails 2 2.5 application where my domain object is not being persisted to the MySQL database and yet no errors are being returned. I've worked with Grails for a number of years so I think I've avoided the more obvious pitfalls, and I've never seen this one before. Here's the code:

if (sale.save(flush:true,failOnError: true)) {
 println "nettTotal = "+sale.nettTotal
 map.response = "OK"
 map.sale = sale
} else {
 map.error = "Unable to save sale"
 sale.errors.allErrors.each {
     println it
 }
}

When I run this, I see the message 'nettTotal = 290.36', showing that the domain object has in fact been saved as far as Grails is concerned (and I don't see any errors which I would do if it had failed). Yet when I do a select in the MySQL database, I see that the row in question remains unchanged ('nett_total' remains NULL, other columns are also unchanged).

The code runs in a controller, without any explicit transaction which might have failed to commit. I'm not sure where else to look. It has to be something simple, but I'm stuck.

(I'm not sure this is relevant, but this application has been proving curiously problematic with spurious errors over the last couple of weeks suggesting some kind of sporadic corruption. For example, previously working findBy* dynamic methods have suddenly started throwing errors, which have been cleared up by a 'deep clean' of the application, i.e., grails clean and the deletion of the project directory in .grails/2.2.5/projects. Yesterday I had a wholly mystifying JSON error from unchanged code which had previously worked, which was once again cleared up by a deep clean. I'm not sure why this is happening, but it worries me and I'd like to prevent it).

1
Have you tried enabling sql logging in your datasource logSql=true ? Might give you some hints - Mike W
Good idea. I just tried that and the result is rather surprising (although consistent with the state of the database). There is no update or insert as a result of the save() call. It's as if it's never happened. There's something clearly wrong there. - John Moore
Incidentally, it's not a general problem - other updates are happening OK. - John Moore
Have your datasources gotten messed up & the app is e.g. using the in memory datasource? - Mike W
No, the datasources are fine. I've found the cause of the problem and how to prevent it, although there is still something clearly wrong. It's in my answer. - John Moore

1 Answers

0
votes

The problem turned out to be with a def beforeUpdate() in the Sale object. There's nothing I can see that's wrong with it, but once I'd commented it out, the update to the database was OK again.