Spring Transaction: rollbackfor and norollback for both defined

Here is the problem I got in the application that I have to support:

I have the first class with the annotation @Transactional(rollbackFor = CustomExceptionA.class) Then in the following code I call the @Transactional(noRollbackFor = CustomExceptionB.class) NB CustomExceptionA : CustomExceptionA or CustomExceptionB have only one common ancestor, which is Exception .

And, of course, when I execute the code, an exception occurs that is neither a type of CustomExceptionA nor CustomExceptionB , and subclasses them.

So the question is simple: What happens to the transaction in this case? Does this do it? Is this a rollback? Does it occupy an unfinished state, waiting for the application to do something (which is actually an answer that might explain some of the ugly things visible in this application)? and what's more: why?

Thanks for your help and time.

+6
source share
1 answer

Spring Transaction Infrastructure Infrastructure Code by default will mark the transaction for rollback in case of runtime, unchecked exceptions; that is, when an exception is thrown, it is an instance or subclass of RuntimeException. (Errors also - by default - lead to rollback.) Checked exceptions that are thrown from the transactional method will not lead to rollback of the transaction.

Why? This makes perfect sense: checked exceptions are mandatory for processing or throwing, so if you selected an excluded exception from the transactional method, then the structure assumes that you know what you are doing. If there is no exception, the exception may be an error or an error handling exception, so the transaction is rolled back to avoid data corruption.

+10
source

Source: https://habr.com/ru/post/951605/


All Articles