Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring's TransactionInterceptor overrides my Exception

I have a DAO class catching the javax.persistence.PersistenceException wrapping them and rethrowing it as a checked exception. This method is marked as @org.springframework.transaction.annotation.Transactional.

If I get an exception in my DAO like constraint violation it will be wrapped in my custom Exception, however spring overrides my exception

[tp1415357209-22] o.s.t.i.TransactionInterceptor           : Application exception overridden by commit exception

and throws it's own org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly

"swallowing" my exception by just logging it and rethrowing its own exception. How can I prevent spring from doing this?

P.S.

@Transactional(noRollbackFor = { MyCustomException.class})

does not work. Of course I do want the transaction to set as rollback but I don't want my exception being swallowed by spring.

like image 626
LAC Avatar asked Nov 23 '15 22:11

LAC


2 Answers

The solution was to use rollbackFor instead of the other attribute so that spring doesn't swallow my own exception

like image 171
LAC Avatar answered Nov 12 '22 02:11

LAC


In case you have a few different Checked Exceptions that you can raise in your class or method, you can use the generic Exception class as well

@Transactional(rollbackFor = Exception.class)
like image 3
smart.aleck Avatar answered Nov 12 '22 01:11

smart.aleck