Skip to content

Commit 50e55d5

Browse files
committed
Polish "Add SAP HANA duplicate key exception error code"
See gh-31554
1 parent fcd4ba2 commit 50e55d5

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ public void translateDuplicateKeyMSSQL2() {
8181
doTest("23000", 2627, DuplicateKeyException.class);
8282
}
8383

84+
@Test
85+
public void translateDuplicateKeySapHana() {
86+
doTest("23000", 301, DuplicateKeyException.class);
87+
}
88+
8489
@Test
8590
public void translateDataAccessResourceFailure() {
8691
doTest("53", DataAccessResourceFailureException.class);

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.r2dbc.connection;
1818

19+
import java.util.Set;
20+
1921
import io.r2dbc.spi.Connection;
2022
import io.r2dbc.spi.ConnectionFactory;
2123
import io.r2dbc.spi.R2dbcBadGrammarException;
@@ -69,6 +71,14 @@ public abstract class ConnectionFactoryUtils {
6971
*/
7072
public static final int CONNECTION_SYNCHRONIZATION_ORDER = 1000;
7173

74+
private static final Set<Integer> DUPLICATE_KEY_ERROR_CODES = Set.of(
75+
1, // Oracle
76+
301, // Sap Hana
77+
1062, // MySQL/MariaDB
78+
2601, // MS SQL Server
79+
2627 // MS SQL Server
80+
);
81+
7282

7383
/**
7484
* Obtain a {@link Connection} from the given {@link ConnectionFactory}.
@@ -255,8 +265,7 @@ else if (ex instanceof R2dbcNonTransientException) {
255265
*/
256266
static boolean indicatesDuplicateKey(@Nullable String sqlState, int errorCode) {
257267
return ("23505".equals(sqlState) ||
258-
("23000".equals(sqlState) &&
259-
(errorCode == 1 || errorCode == 1062 || errorCode == 2601 || errorCode == 2627)));
268+
("23000".equals(sqlState) && DUPLICATE_KEY_ERROR_CODES.contains(errorCode)));
260269
}
261270

262271
/**

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ public void shouldTranslateIntegrityViolationException() {
100100
new R2dbcDataIntegrityViolationException("reason", "23000", 1));
101101
assertThat(exception).isExactlyInstanceOf(DuplicateKeyException.class);
102102

103+
exception = ConnectionFactoryUtils.convertR2dbcException("", "",
104+
new R2dbcDataIntegrityViolationException("reason", "23000", 301));
105+
assertThat(exception).isExactlyInstanceOf(DuplicateKeyException.class);
106+
103107
exception = ConnectionFactoryUtils.convertR2dbcException("", "",
104108
new R2dbcDataIntegrityViolationException("reason", "23000", 1062));
105109
assertThat(exception).isExactlyInstanceOf(DuplicateKeyException.class);

0 commit comments

Comments
 (0)