无法使用包装的事务捕获 DataIntegrityViolationException
我正在使用 Spring 3 来管理数据库事务。每当引发异常时,我都会捕获异常并返回相应的消息。但是当 Spring 提交事务时,异常会再次引发并包装在 org.springframework.dao.DataIntegrityValidationException 中。我该如何处理该异常?我应该使用拦截器还是其他东西?以前有人遇到过这个问题吗?
提前致谢
I'm using Spring 3 for managing DB transactions. Whenever an exception is raised, I catch the exception and return the corresponding message. But when Spring commits the transaction, the exception is raised again and wrapped in a org.springframework.dao.DataIntegrityValidationException
. How can I handle that exception? should I use interceptors or something else? Has anyone already had this problem before?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的事务边界位于服务层(如果您有一个,您应该这样做),那么您应该捕获边界之外的异常。 Spring 的工作方式是,如果异常在事务边界之外冒泡,则事务将回滚。通过捕获异常,您将停止该过程,这可能是您不想要的。
拦截器是处理事务边界之外的异常的好方法。
If your transaction boundaries are at your service layer (if you have one, which you should), then you should catch the exception outside of the boundary. The way Spring works is if an exception bubbles outside the transaction boundary, the transaction is rolled back. By catching the exception you are stopping that process, which you probably dont want.
Interceptors are a good way to deal with the exception outside of the transaction boundary.