Incorrect exception type throw by spring jdbc #23675
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
Affects: 5.1.9 and previous versions
spring-jdbc
usesSQLErrorCodeSQLExceptionTranslator
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
invokesJdbcUtils#extractDatabaseMetaData(dataSource, "getDatabaseProductName")
which then callsJdbcUtils.extractDatabaseMetaData(DataSource, DatabaseMetaDataCallback)
. The call toJdbcUtils#extractDatabaseMetaData
creates a connection using thedatasource
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, thenSQLErrorCodesFactory
will never try to re-initializeSQLErrorCodes
and then for the entire life cycle of the application Spring will fall back to theSQLExceptionSubclassTranslator
.This causes Spring to throw inconsistent exceptions. For example, if mysql throws 1062 and
SQLErrorCodeSQLExceptionTranslator
is not properly initialized due to connection issue, thenSQLExceptionSubclassTranslator
will throwDataIntegrityViolationException
instead ofDuplicateKeyException
, 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 initializedSQLErrorCodes
will throw a different exception from a node which is falling back toSQLExceptionSubclassTranslator
.The text was updated successfully, but these errors were encountered: