我的Web应用程序在Spring(MVC)4.2.9.RELEASE,Hibernate 5.1.3.Final,Spring Data 1.8.2.RELEASE和MS SQL Server(2014)上运行。
在Spring的背景下,我有以下例外情况:
<bean id="simpleMappingExceptionResolver" class="myproject.CustomMappingExceptionResolver">
...
</bean>
捕获并保存堆栈跟踪。我能够在日志中打印的长堆栈跟踪中看到以下深:
......
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
... 113 more
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Transaction (Process ID 73) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:258)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1535)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:467)
如何在上面的例外汉字中找到以下异常类(并给出一个异常实例):
com.microsoft.sqlserver.jdbc.SQLServerException
AND 相应的消息:
Transaction (Process ID 73) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
答案 0 :(得分:1)
如果我理解正确,您需要捕获嵌套异常。这有点棘手,但可行。您需要为顶级例外设置try-catch
块。在catch子句中,您可以使用exception.getCause()
一次降低一个嵌套级别,并查看该级别是否为{1}} SQL异常类。如有必要,您还可以使用instanceof
查看邮件。如果异常符合您的标准,恭喜您抓住它。如果没有,只需再次getMessage()
。
要记住两件事:
throw
将返回e.getCause()
本身。注意这里的无限递归。