Spring's TransactionInterceptor overrides my Exception

China☆狼群 提交于 2019-12-23 11:59:19

问题


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.


回答1:


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




回答2:


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)


来源:https://stackoverflow.com/questions/33881648/springs-transactioninterceptor-overrides-my-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!