Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect exception type throw by spring jdbc #23675

Closed
samratdhillon opened this issue Sep 23, 2019 · 1 comment
Closed

Incorrect exception type throw by spring jdbc #23675

samratdhillon opened this issue Sep 23, 2019 · 1 comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: superseded An issue that has been superseded by another type: enhancement A general enhancement

Comments

@samratdhillon
Copy link

samratdhillon commented Sep 23, 2019

Affects: 5.1.9 and previous versions

spring-jdbc uses SQLErrorCodeSQLExceptionTranslator with mapping file /spring-jdbc/src/main/resources/org/springframework/jdbc/support/sql-error-codes.xml to map between vendor error codes and relevant Exceptions.

For example, if underlying database is mysql then vendor error code 1062 gets mapped to DuplicateKeyException.

However to be able to identify the vendor itself spring-jdbc invokes JdbcUtils#extractDatabaseMetaData(dataSource, "getDatabaseProductName") which then calls JdbcUtils.extractDatabaseMetaData(DataSource, DatabaseMetaDataCallback). The call to JdbcUtils#extractDatabaseMetaData creates a connection using the datasource and then retrieves the database vendor (although vendor name could be available from the driver itself and creating connection may not be necessary). If however creating the connection fails for whatever reason, then SQLErrorCodesFactory will never try to re-initialize SQLErrorCodes and then for the entire life cycle of the application Spring will fall back to the SQLExceptionSubclassTranslator.

This causes Spring to throw inconsistent exceptions. For example, if mysql throws 1062 and SQLErrorCodeSQLExceptionTranslator is not properly initialized due to connection issue, then SQLExceptionSubclassTranslator will throw DataIntegrityViolationException instead of DuplicateKeyException, and this will continue until the application is restarted.

Ideally Spring should keep on retrying to discover vendor name if JdbcUtils#extractDatabaseMetaData fails. This can be very annoying in a cluster where one node with properly initialized SQLErrorCodes will throw a different exception from a node which is falling back to SQLExceptionSubclassTranslator.

@snicoll
Copy link
Member

snicoll commented Sep 23, 2019

@samratdhillon thanks for the PR. We don't need both an issue and a PR as one supersedes the other. Let's continue this conversation on the PR.

Closing in favour of PR #23676

@snicoll snicoll closed this as completed Sep 23, 2019
@snicoll snicoll added status: superseded An issue that has been superseded by another and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Sep 23, 2019
@sbrannen sbrannen added in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement labels Sep 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: superseded An issue that has been superseded by another type: enhancement A general enhancement
Projects
None yet
4 participants