I have a method, inside a Try/Catch block, which calls a Hibernate DB Save operation to insert a row.
The method completes successfully even though there are DB problems (e.g. when I insert a NULL into a non-NULL column). Then, at some later point, Hibernates attempts to “flush” or complete the transaction, and that’s when errors get thrown.
This messes up the flow of my code because I depend on my method completing successfully to do other things, e.g. send out emails. After calling my method, I go on to send out emails based on the assumption that no errors have happened (otherwise I would have been thrown out of my code flow and into my Catch block, but this is not happening).
Does anyone have any ideas how to deal with this situation?
The trivial answer is to simply call
Session.flush()and any pending SQL will get run, causing any SQL exceptions that might be lurking to happen at that time.On a sort of larger scope, you may want to look at options for validating your data at the application level, rather than relying on SQL exceptions to detect errors. There are up and downsides to either way of course.