Skip to content

Commit ca94396

Browse files
committed
Detect MSSQL code 2601 as duplicate key (aligned with sql-error-codes.xml)
Closes gh-29950
1 parent 5819d76 commit ca94396

File tree

5 files changed

+18
-8
lines changed

5 files changed

+18
-8
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/support/SQLStateSQLExceptionTranslator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -153,14 +153,14 @@ private String getSqlState(SQLException ex) {
153153
* of a generic SQL state value) indicate a duplicate key exception:
154154
* either SQL state 23505 as a specific indication, or the generic SQL state
155155
* 23000 with well-known vendor codes (1 for Oracle, 1062 for MySQL/MariaDB,
156-
* 2627 for MS SQL Server).
156+
* 2601/2627 for MS SQL Server).
157157
* @param sqlState the SQL state value
158158
* @param errorCode the error code value
159159
*/
160160
static boolean indicatesDuplicateKey(@Nullable String sqlState, int errorCode) {
161161
return ("23505".equals(sqlState) ||
162162
("23000".equals(sqlState) &&
163-
(errorCode == 1 || errorCode == 1062 || errorCode == 2627)));
163+
(errorCode == 1 || errorCode == 1062 || errorCode == 2601 || errorCode == 2627)));
164164
}
165165

166166
}

spring-jdbc/src/test/java/org/springframework/jdbc/support/SQLExceptionSubclassTranslatorTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public void exceptionClassTranslation() {
5959
doTest(new SQLIntegrityConstraintViolationException("", "23505", 0), DuplicateKeyException.class);
6060
doTest(new SQLIntegrityConstraintViolationException("", "23000", 1), DuplicateKeyException.class);
6161
doTest(new SQLIntegrityConstraintViolationException("", "23000", 1062), DuplicateKeyException.class);
62+
doTest(new SQLIntegrityConstraintViolationException("", "23000", 2601), DuplicateKeyException.class);
6263
doTest(new SQLIntegrityConstraintViolationException("", "23000", 2627), DuplicateKeyException.class);
6364
doTest(new SQLInvalidAuthorizationSpecException("", "", 0), PermissionDeniedDataAccessException.class);
6465
doTest(new SQLNonTransientConnectionException("", "", 0), DataAccessResourceFailureException.class);

spring-jdbc/src/test/java/org/springframework/jdbc/support/SQLStateSQLExceptionTranslatorTests.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -72,7 +72,12 @@ public void translateDuplicateKeyMySQL() {
7272
}
7373

7474
@Test
75-
public void translateDuplicateKeyMSSQL() {
75+
public void translateDuplicateKeyMSSQL1() {
76+
doTest("23000", 2601, DuplicateKeyException.class);
77+
}
78+
79+
@Test
80+
public void translateDuplicateKeyMSSQL2() {
7681
doTest("23000", 2627, DuplicateKeyException.class);
7782
}
7883

spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/ConnectionFactoryUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -256,7 +256,7 @@ else if (ex instanceof R2dbcNonTransientException) {
256256
static boolean indicatesDuplicateKey(@Nullable String sqlState, int errorCode) {
257257
return ("23505".equals(sqlState) ||
258258
("23000".equals(sqlState) &&
259-
(errorCode == 1 || errorCode == 1062 || errorCode == 2627)));
259+
(errorCode == 1 || errorCode == 1062 || errorCode == 2601 || errorCode == 2627)));
260260
}
261261

262262
/**

spring-r2dbc/src/test/java/org/springframework/r2dbc/connection/ConnectionFactoryUtilsUnitTests.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2022 the original author or authors.
2+
* Copyright 2019-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -104,6 +104,10 @@ public void shouldTranslateIntegrityViolationException() {
104104
new R2dbcDataIntegrityViolationException("reason", "23000", 1062));
105105
assertThat(exception).isExactlyInstanceOf(DuplicateKeyException.class);
106106

107+
exception = ConnectionFactoryUtils.convertR2dbcException("", "",
108+
new R2dbcDataIntegrityViolationException("reason", "23000", 2601));
109+
assertThat(exception).isExactlyInstanceOf(DuplicateKeyException.class);
110+
107111
exception = ConnectionFactoryUtils.convertR2dbcException("", "",
108112
new R2dbcDataIntegrityViolationException("reason", "23000", 2627));
109113
assertThat(exception).isExactlyInstanceOf(DuplicateKeyException.class);

0 commit comments

Comments
 (0)