Skip to content

Sort duplicate key codes in SQLErrorCodes - #37235

Closed
junhyeong9812 wants to merge 1 commit into
spring-projects:mainfrom
junhyeong9812:fix/sqlerrorcodes-sort-duplicate-key-codes
Closed

junhyeong9812 wants to merge 1 commit into
spring-projects:mainfrom
junhyeong9812:fix/sqlerrorcodes-sort-duplicate-key-codes

Conversation

@junhyeong9812

Copy link
Copy Markdown
Contributor

Overview

SQLErrorCodes.setDuplicateKeyCodes is the only error code setter that does not sort the supplied array, while SQLErrorCodeSQLExceptionTranslator looks all code arrays up with Arrays.binarySearch. With an unsorted custom list of duplicate key codes, translation silently succeeds or fails depending on where each value happens to sit in the array. This PR makes the setter sort the array like its nine sibling setters.

Problem

All other code array setters in SQLErrorCodes store StringUtils.sortStringArray(...), and CustomSQLErrorCodesTranslation.setErrorCodes does the same, establishing the invariant that Arrays.binarySearch relies on. setDuplicateKeyCodes stores the array as-is.

Binary search on an unsorted array is data-dependent: with setDuplicateKeyCodes("90002", "1586", "1062"), error code 1586 happens to be found (it sits at the first probed middle position), while 90002 and 1062 are missed because the comparison at the middle element steers the search into the wrong half. For a missed code the translator falls through: with a generic 23xxx SQLState the fallback reports a DataIntegrityViolationException, and without a usable SQLState the translation returns no result at all, instead of the configured DuplicateKeyException.

The default sql-error-codes.xml is unaffected because its duplicateKeyCodes lists are already lexicographically sorted. The mismatch surfaces for custom configurations, which nothing instructs to pre-sort - most naturally when codes of different digit lengths are listed in numeric order (for example 548, 2601, 2627, which is unsorted as strings), or when application-specific codes are appended to a vendor list.

Fix

setDuplicateKeyCodes now applies StringUtils.sortStringArray like all sibling setters, so lookups no longer depend on the declaration order. The new test configures a translator with an unsorted custom list and verifies that all three codes translate to DuplicateKeyException; before the change, two of the three lookups missed. The full spring-jdbc test suite passes.

Every error code setter in SQLErrorCodes sorts its array with
StringUtils.sortStringArray, and CustomSQLErrorCodesTranslation does
the same, because SQLErrorCodeSQLExceptionTranslator looks the codes
up with Arrays.binarySearch. setDuplicateKeyCodes was the only setter
that stored the supplied array as-is.

With an unsorted list of duplicate key codes, the binary search finds
or misses a code depending on where the values happen to sit: for
codes it misses, the translator silently falls through to the SQLState
fallback and reports a DataIntegrityViolationException, or fails to
translate at all, instead of the configured DuplicateKeyException. The
default sql-error-codes.xml is not affected since its lists are
already sorted; the mismatch surfaces for custom configurations, for
example codes of different digit lengths listed in numeric order.

setDuplicateKeyCodes now sorts the array like all sibling setters. The
new test covers an unsorted custom list whose codes previously hit or
missed depending on their position.

Signed-off-by: junhyeong9812 <pickjog@gmail.com>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Sep 4, 2026
@bclozel bclozel added the in: data Issues in data modules (jdbc, orm, oxm, tx) label Sep 4, 2026
@sbrannen sbrannen self-assigned this Sep 4, 2026
@sbrannen sbrannen added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Sep 4, 2026
@sbrannen sbrannen added this to the 7.0.10 milestone Sep 4, 2026
@sbrannen sbrannen changed the title Sort duplicate key codes in SQLErrorCodes Sort duplicate key codes in SQLErrorCodes Sep 5, 2026
sbrannen pushed a commit that referenced this pull request Sep 5, 2026
Every error code setter in SQLErrorCodes sorts its array with
StringUtils.sortStringArray, and CustomSQLErrorCodesTranslation does
the same, because SQLErrorCodeSQLExceptionTranslator looks the codes
up with Arrays.binarySearch. setDuplicateKeyCodes was the only setter
that stored the supplied array as-is.

With an unsorted list of duplicate key codes, the binary search finds
or misses a code depending on where the values happen to sit: for
codes it misses, the translator silently falls through to the SQLState
fallback and reports a DataIntegrityViolationException, or fails to
translate at all, instead of the configured DuplicateKeyException. The
default sql-error-codes.xml is not affected since its lists are
already sorted; the mismatch surfaces for custom configurations, for
example codes of different digit lengths listed in numeric order.

setDuplicateKeyCodes now sorts the array like all sibling setters. The
new test covers an unsorted custom list whose codes previously hit or
missed depending on their position.

Closes gh-37235

Signed-off-by: junhyeong9812 <pickjog@gmail.com>
@github-actions

github-actions Bot commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

Fixed via 6e260bc

@github-actions github-actions Bot closed this Sep 5, 2026
@sbrannen

sbrannen commented Sep 5, 2026

Copy link
Copy Markdown
Member

This has been merged into 7.0.x and main.

Thanks

@junhyeong9812

Copy link
Copy Markdown
Contributor Author

Thanks for applying this to 7.0.x and main.

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: bug A general bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants