Reputation: 317
I am experimenting with @transactional
in spring boot. I have written a service with multiple table insertions and I want the entire thing to rollback if there is any exception.
This works fine but rather than throwing the actual exception that caused the rollback, I am getting Unexpected Rollback Exception
@transactional
public void insertIntoTables(){
repositoryone.save(new Table1());
repositorytwo.save(new Table2()); //expecting data integrity violation exception on this line
}
Rather than throwing the DataIntegrityViolationException
, the above code throws Unexpected Rollback Exception
. I want the rollback to happen ofcourse, just want to avoid the exception so that I can use the actual one.
How to get the actual exception for handling purposes?
Upvotes: 1
Views: 2883
Reputation: 1118
Even I faced similar issue before. I handled it by adding rollback for. Below is how I handled it.
This will handle any runtime exception. So, DataIntegrityViolationException should be displayed to user.
Upvotes: 2