Reputation: 3841
Our application is highly concurrent. It is taking MQ data and saving it to a database. There are multiple messages that might contain the same data. So one message might contain operating hours and the second message might contain operating hours and location.
So the first message comes through and I save the operating hours data. Then the second transaction comes in with the same exact data for operating hours. Now I need the location data to be saved, but the operating hours is a duplicate. I can't make a check for operating hours on the database because of concurrency (If I check, it might so it is not existing, then by the time I save it, it might have been inserted by another thread). I also want to be fast. (lots of load on this loader.)
Currently a message gets one Transaction (Propagation.REQUIRED) (using Spring and Hibernate). I do a flush after each kind of data insert. So I save the location data, and do a flush. then I save the operating hours and flush.
If I just catch the error message (it is thrown after the flush()) and eat it, will the other data saved (the location data) still be saved? Is the Transaction still OK to use after a database insert failure? So if the first save failed, I want the second insert to work.
I have been looking at Propagation.REQUIRES_NEW but have had some trouble. Also, I am concerned about slowing this down. I would need 3 transactions (there is a bit of logic before all this.)
Most of the info on the we talks about Rollbacks, but I don't want to rollback. I just want to move on. Can I?
Upvotes: 2
Views: 2548
Reputation: 691715
No, you can't catch the exception and try to continue. Here's what the chapter about exception handling of the hibernate documentation says:
If the Session throws an exception, including any SQLException, immediately rollback the database transaction, call Session.close() and discard the Session instance. Certain methods of Session will not leave the session in a consistent state. No exception thrown by Hibernate can be treated as recoverable.
Upvotes: 3