Skip to content

Duplicate key violation gets translated to DataIntegrityViolationException instead of DuplicateKeyException in Spring 6 #29511

@albertus82

Description

@albertus82

Affects: 6.0.0


Hi, I just upgraded from 5.3.24 to 6.0.0 and noticed a different behaviour of Spring JDBC/TX. I have a class containing simple JDBC statements executed with JdbcTemplate (no JPA, no ORMs), and I wrote some test cases based on H2.

In case of duplicate key errors, I expect DuplicateKeyException, but after the upgrade I'm getting a more generic DataIntegrityViolationException (that obviously breaks the test):

org.springframework.dao.DataIntegrityViolationException: PreparedStatementCallback; SQL [INSERT INTO "STORAGE" (filename, last_modified, compressed, file_contents) VALUES (?, ?, ?, ?)]; Unique index or primary key violation: "PUBLIC.PRIMARY_KEY_B ON PUBLIC.STORAGE(FILENAME) VALUES ( /* 1 */ 'myfile.txt' )"; SQL statement:
INSERT INTO "STORAGE" (filename, last_modified, compressed, file_contents) VALUES (?, ?, ?, ?) [23505-214]

Looking into the sources, I noticed that v5 instantiates a DuplicateKeyException in SQLErrorCodeSQLExceptionTranslator while v6 instantiates a DataIntegrityViolationException in SQLExceptionSubclassTranslator.

Thank you.

Metadata

Metadata

Assignees

Labels

in: dataIssues in data modules (jdbc, orm, oxm, tx)type: regressionA bug that is also a regressiontype: wiki-documentationA documentation update required on the wiki

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions