I’ve had a great run with Grails. It allows me to create some quick tools, with a web interface. However, I recently had a bought with Grails that left me pretty pissed off.
Let it be known that I’m not a Grails dev with lots of experience. I’m an SDET/Tools dev/QA guy. The idea of transactions wasn’t something I was well read on. In the past I’ve had Grails services that auto annotate as @transactional and there was never a problem for me to create a domain object and save an update to it, from the service.
Recently however, I had a new piece of functionality. Every few seconds, I pull a list of servers in my tool. I iterate over the server list appending an endpoint that will provide JSON with relevant health data. I take that and save it to a db, and a front end pulls from the db to show the results in a UI.
This seemed easy, in fact I got it all together but alas, the UI wasn’t updating with the data points from the [server]/[endpoint]. I put in some debug code and clearly saw that the data was getting consumed fine. So I thought the issue was with how I was saving to the domain object.
I tried different ways of creating it… I thought I had a odd constraint problem… no matter what I did, one thing was clear:
no errors anywhere.
Even with .save(failOnError:true) it was still running without errors and yet the data wasn’t persisting. I tried using GORM syntax, executeUpdate syntax, even tried inserting into another table, thinking I was somehow deadlocking and not getting warned bout it… but nothing worked.
I followed online solutions after solutions until I realized I was getting far afield.
The crazy part was when I put some debug code that checked the database for my entry post save. In other words:
I save an update in Grails
Then I check the table for the update I just saved.
It was blowing my mind that it was coming back with the right value – but the Database when queried directly was showing no such commit…
After talking with a developer he told me to look into:
Doing so, revealed some posts about “your data won’t save unless it’s transactional. Best to use a service.” Well I was using a service, and it was tagged with the @transactional annotation.
Then I came across:
There someone mentioned the use of:
OK, now that worked. all of a sudden I was getting my data updated.
But Did I need a Transaction?
The dev I talked to asked me a few questions… and finally said, “I don’t think you need a transaction here…” He explained what was going on, and he’s right… I had no need of a rollback or a transaction state. In my particular use case this was overkill….
By removing the @Transactional and @Transactional(propagation=Propagation.REQUIRES_NEW) from the service, it was still working. So I left it out since it was overkill in my needs.
This was a very frustrating problem for me, as I wasn’t clear where the problem lay. Having no feedback from Grails made it even more difficult. I don’t even know what prompted me to check the database for the save/updated data I was doing, but that was the final key to unlock the problem.