Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Uncategorized data access exception thrown for deadlock [SPR-2617] #7306
During update by using JdbcTemplate.update(String sql, Object args), I encounter exception due to deadlock. This is thrown as an SQLException. Exception log is
exception: PreparedStatementCallback; uncategorized SQLException for SQL [UPDATE EMP_INFO SET ORG_NAME=?, EMP_MAME=?, WHERE EMP_ID=?]; SQL state ; error code ; Transaction (Process ID 58) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.; nested exception is java.sql.SQLException: Transaction (Process ID 58) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.
In the above scenario, I encounter couple of issues:
Erik Hoogeveen commented
You can capture the SQLException by implementing the SQLExceptionTranslator interface somewhere. This implementation should translate the SQL exception to a specific exception that extends from DataAccessException using the SQL error code. For example in this case it should translate SQLException whit error code 1205 to a DeadlockLoserDataAccessException.
Once the translation is in place you can catch the exceptions you're interested in.
btw, since this is no bug but a usage issue. Please post it to the support forum (http://forum.springframework.org/) next time around. JIRA is meant for bug reports. Not for support.
Juergen Hoeller commented
Indeed, you should be able to catch DataAccessException and check the underlying SQLException root cause there (simply do a getCause call).
As for the exception being thrown as uncategorized, we probably just need to add the specific error code to our default sql-error-codes.xml file (in the org.springframework.jdbc.support package). You may also adapt that file yourself and put it into the root of the classpath; the default exception translator will pick it up there.
Which database are you using there? Let me know; I'll try to extend its deadlock error codes in our default sql-error-codes.xml file then. For that reason, I'm turning this issue into an enhancement request.
John Nutting commented
We are also seeing this behavior in our application using JPA with Hibernate and the Merlia driver.
In the stack trace below, it looks like the correct error code (1205) is making its way to the SQLStateConverter. I started to wonder whether our DatabaseProductName is not being correctly set to "Microsoft SQL Server", but other exceptions appear to be correctly translated, such as dataIntegrityViolationCodes, so this does not seem likely.
Stack trace excerpt:
com.hare.feed.FeedProcessFatalException: org.springframework.orm.hibernate3.HibernateJdbcException: JDBC exception on Hibernate data access: SQLException for SQL [select listing0_.ListingGUID as Listing ... order by listing0_.createDate desc]; SQL state ; error code ; could not execute query; nested exception is org.hibernate.exception.GenericJDBCException: could not execute query at com.hare.feed.AbstractFeedProcess.processXMLListing(AbstractFeedProcess.java:1285)
Pertinent Spring beans:
John Nutting commented
I left out the part of the stack trace where the Spring translation is being done. Here's a more complete one:
com.hare.feed.FeedProcessFatalException: org.springframework.orm.hibernate3.HibernateJdbcException: JDBC exception on Hibernate data access: SQLException for SQL [select listing0_.ListingGUID as ListingG1_3_0_, ... order by listing0_.createDate desc]; SQL state ; error code ; could not execute query; nested exception is org.hibernate.exceptio
Thomas Risberg commented
In the Hibernate/JPA case it is Hibernate that does the translation into a org.hibernate.exception.GenericJDBCException which in turn gets translated to a org.springframework.orm.hibernate3.HibernateJdbcException which extends UncategorizedDataAccessException. So the sql-error-codes.sql is never used for this scenario. The original question was regarding JDBC usage and in this case the sql-error-codes.xml is used to translate and the resulting exception should be a DeadlockLoserDataAccessException.
Original posting is about 3 years old now and we have added the 1205 code to the translation. I think it's safe to finally close this issue.