Spring的TransactionInterceptor覆盖了我的异常 [英] Spring's TransactionInterceptor overrides my Exception

查看:69
本文介绍了Spring的TransactionInterceptor覆盖了我的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DAO类,它捕获 javax.persistence.PersistenceException 并将它们包装起来,然后将其作为经过检查的异常重新抛出.此方法标记为 @ org.springframework.transaction.annotation.Transactional .

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.

如果我在DAO中遇到异常(例如违反约束),它将包装在我的自定义异常中,但是spring会覆盖我的异常

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

并抛出它自己的 org.springframework.transaction.TransactionSystemException:无法提交JPA事务;嵌套的异常是javax.persistence.RollbackException:标记为rollbackOnly的事务

通过记录异常并重新抛出其自身的异常来吞噬"我的异常.如何防止spring这样做?

"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.

推荐答案

解决方案是使用 rollbackFor 而不是其他属性,以便spring不会吞噬我自己的异常

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

这篇关于Spring的TransactionInterceptor覆盖了我的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆