-
Notifications
You must be signed in to change notification settings - Fork 38.8k
Description
Francisco Lozano opened SPR-11959 and commented
With SQLErrorCodeSQLExceptionTranslator, my code can catch both DuplicateKeyException and DataIntegrityViolationException. This helps to distinguish between a FK constraint violation and a duplicate key, directly and without having to dig more onto the database to find out.
I had to change my use of JdbcTemplate to force SQLExceptionSubclassTranslator, in order to gracefully handle query timeouts. Unfortunately, this had the effect of rendering all my catches of DuplicateKeyException unreachable.
So, by default and without coding anything, as a user of Spring JDBC wrappers I'm stuck between a rock and a hard place.
I guess the workaround is to make a custom SQLExceptionTranslator, but this use-case is not so strange and it would be great if Spring could come with a "smart" SQL exception translator that uses whatever it needs to provide the most accurate exception possible.
I'm not sure now this should be reported a "bug" or as something else... I initially considered SQLErrorCodeSQLExceptionTranslator "buggy" because it is not translating exceptions onto DuplicateKeyException, but I can see in this case it's not straightforward to do it right for everyone.
Affects: 4.0.5
Issue Links:
- java.sql.SQLTimeoutException not translated to org.springframework.dao.QueryTimeoutException [SPR-9376] #14012 java.sql.SQLTimeoutException not translated to org.springframework.dao.QueryTimeoutException
Referenced from: commits 4082274