Two Spring/Hibernate gotchas that got me
Earlier this week I got caught out by two Spring/Hibernate features. One of my service layer methods threw a checked exception after a bit of relatively complex validation, just like I intended. What happened next, however, was not expected. My test failed. Well, it didn't fail, it died with an error and a horrible stack trace of doom. Hibernate was kind enough to tell me that a batch update failed. This struck me as slightly odd, considering the fact that an exception was thrown long before any saving was meant to occur.
I had, apparently, forgotten about the rather unintuitive automagic dirty checking and subsequent saving. In my rather humble opinion, it's a feature that should be disabled by default, but that's not the point here. I had been made aware of the feature in the past, but had somehow completely forgotten about it. Fine, no worries, I can live with it, I'll just roll back my transaction.
However! My test still shouldn't have failed, or so I thought. After all, I had Spring transaction support set up for all my service calls, and surely an exception will cause the transaction to roll back? Wrong. By default, transactions are only rolled back for Unchecked exceptions, not for checked exceptions. I can't for the life of me fathom what the reasoning behind this decision might be, but there you have it. It's well document in the Spring transaction documentation, but again I had completely forgotten about it.
A simple bit of config fixed the problem:
<tx:method name="*" rollback-for="java.lang.Throwable"/>
So please, don't be like me. Don't get caught out by these features! Unless you're looking for a couple of hours of entertainment, that is
.
