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

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

Closed
ghost opened this issue Feb 9, 2023 · 1 comment
Assignees
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) type: regression A bug that is also a regression
Milestone

Comments

@ghost
Copy link

ghost commented Feb 9, 2023

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

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Feb 9, 2023
@sbrannen sbrannen added in: data Issues in data modules (jdbc, orm, oxm, tx) type: regression A bug that is also a regression labels Feb 9, 2023
@jhoeller jhoeller removed the status: waiting-for-triage An issue we've not yet triaged or decided on label Feb 9, 2023
@jhoeller jhoeller self-assigned this Feb 9, 2023
@jhoeller jhoeller added this to the 6.0.5 milestone Feb 9, 2023
@jhoeller
Copy link
Contributor

jhoeller commented Feb 9, 2023

Good point, we translate 2601 to duplicate key in sql-error-codes.xml as well (per default before 6.0). I'll close that gap for 6.0.5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: data Issues in data modules (jdbc, orm, oxm, tx) type: regression A bug that is also a regression
Projects
None yet
Development

No branches or pull requests

3 participants