diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/support/SQLErrorCodes.java b/spring-jdbc/src/main/java/org/springframework/jdbc/support/SQLErrorCodes.java index 34d4ca66108..e2276c3c4c5 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/support/SQLErrorCodes.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/support/SQLErrorCodes.java @@ -123,7 +123,7 @@ public class SQLErrorCodes { } public void setDuplicateKeyCodes(String... duplicateKeyCodes) { - this.duplicateKeyCodes = duplicateKeyCodes; + this.duplicateKeyCodes = StringUtils.sortStringArray(duplicateKeyCodes); } public void setDataIntegrityViolationCodes(String... dataIntegrityViolationCodes) { diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/support/SQLErrorCodeSQLExceptionTranslatorTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/support/SQLErrorCodeSQLExceptionTranslatorTests.java index 1f02db5aca6..3e96ff7f33f 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/support/SQLErrorCodeSQLExceptionTranslatorTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/support/SQLErrorCodeSQLExceptionTranslatorTests.java @@ -107,6 +107,20 @@ class SQLErrorCodeSQLExceptionTranslatorTests { .hasCause(sqlException); } + @Test // gh-37235 + void duplicateKeyCodesDeclaredInUnsortedOrder() { + SQLErrorCodes errorCodes = new SQLErrorCodes(); + errorCodes.setDuplicateKeyCodes("90002", "1586", "1062"); + SQLErrorCodeSQLExceptionTranslator unsortedCodesTranslator = new SQLErrorCodeSQLExceptionTranslator(errorCodes); + + for (int errorCode : new int[] {90002, 1586, 1062}) { + SQLException sqlException = new SQLException("", "", errorCode); + assertThat(unsortedCodesTranslator.translate("task", "SQL", sqlException)) + .isInstanceOf(DuplicateKeyException.class) + .hasCause(sqlException); + } + } + @Test void batchExceptionTranslation() { SQLException badSqlEx = new SQLException("", "", 1);