Spring's TransactionInterceptor overrides my Exception

LAC picture LAC · Nov 23, 2015 · Viewed 10.1k times · Source

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.

Answer

LAC picture LAC · Nov 26, 2015

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