Skip to content

Violation of a unique index on insert in MSSQL is translated to DataIntegrityViolationException instead of DuplicateKeyException #29950

Closed
@ghost

Description

Affects: 6.0.4 (spring-r2dbc)

When inserting a row in MSSQL, using spring-r2dbc, that results in a unique constraint violation, the thrown exception is DataIntegrityViolationException.

I would have expected a DuplicateKeyException, for its Javadoc documentation states:

Exception thrown when an attempt to insert or update data results in violation of a primary key or unique constraint.

The SQL state and code returned by MSSQL are the following:

sqlState: 23000
errorCode: 2601

The method ConnectionFactoryUtils.indicatesDuplicateKey() does not seem to take into account the 2601 error code.

The exception thrown has the following stack trace:

org.springframework.dao.DataIntegrityViolationException:
    executeMany; SQL [INSERT INTO MY_TABLE (my_column) VALUES (@P0_mycolumn)]; 
    Cannot insert duplicate key row in object 'dbo.MY_TABLE' with unique index 'IDX_MY_TABLE_MY_COLUMN'.
    The duplicate key value is (-1).
	at org.springframework.r2dbc.connection.ConnectionFactoryUtils.convertR2dbcException(ConnectionFactoryUtils.java:237) ~[spring-r2dbc-6.0.4.jar:6.0.4]

Note that it also affects spring-jdbc when using JdbcTemplate, for the error translation done in SQLStateSQLExceptionTranslator.indicatesDuplicateKey() is pretty much the same.

This might also relates to the following issue: #29699

Metadata

Metadata

Assignees

Labels

in: dataIssues in data modules (jdbc, orm, oxm, tx)type: regressionA bug that is also a regression

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions