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: https://github.com/spring-projects/spring-framework/issues/29699
Comment From: jhoeller
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.