Browse Source

Merge branch '6.0.x'

pull/31571/head
Stéphane Nicoll 1 year ago
parent
commit
c30b3796f2
  1. 14
      spring-jdbc/src/main/java/org/springframework/jdbc/support/SQLStateSQLExceptionTranslator.java
  2. 5
      spring-jdbc/src/test/java/org/springframework/jdbc/support/SQLStateSQLExceptionTranslatorTests.java
  3. 13
      spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/ConnectionFactoryUtils.java
  4. 4
      spring-r2dbc/src/test/java/org/springframework/r2dbc/connection/ConnectionFactoryUtilsUnitTests.java

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

@ -89,6 +89,14 @@ public class SQLStateSQLExceptionTranslator extends AbstractFallbackSQLException @@ -89,6 +89,14 @@ public class SQLStateSQLExceptionTranslator extends AbstractFallbackSQLException
"61" // Oracle: deadlock
);
private static final Set<Integer> DUPLICATE_KEY_ERROR_CODES = Set.of(
1, // Oracle
301, // Sap Hana
1062, // MySQL/MariaDB
2601, // MS SQL Server
2627 // MS SQL Server
);
@Override
@Nullable
@ -158,15 +166,13 @@ public class SQLStateSQLExceptionTranslator extends AbstractFallbackSQLException @@ -158,15 +166,13 @@ public class SQLStateSQLExceptionTranslator extends AbstractFallbackSQLException
* Check whether the given SQL state (and the associated error code in case
* of a generic SQL state value) indicate a {@link DuplicateKeyException}:
* either SQL state 23505 as a specific indication, or the generic SQL state
* 23000 with well-known vendor codes (1 for Oracle, 1062 for MySQL/MariaDB,
* 2601/2627 for MS SQL Server).
* 23000 with well-known vendor codes.
* @param sqlState the SQL state value
* @param errorCode the error code value
*/
static boolean indicatesDuplicateKey(@Nullable String sqlState, int errorCode) {
return ("23505".equals(sqlState) ||
("23000".equals(sqlState) &&
(errorCode == 1 || errorCode == 1062 || errorCode == 2601 || errorCode == 2627)));
("23000".equals(sqlState) && DUPLICATE_KEY_ERROR_CODES.contains(errorCode)));
}
/**

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

@ -81,6 +81,11 @@ public class SQLStateSQLExceptionTranslatorTests { @@ -81,6 +81,11 @@ public class SQLStateSQLExceptionTranslatorTests {
doTest("23000", 2627, DuplicateKeyException.class);
}
@Test
public void translateDuplicateKeySapHana() {
doTest("23000", 301, DuplicateKeyException.class);
}
@Test
public void translateDataAccessResourceFailure() {
doTest("53", DataAccessResourceFailureException.class);

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

@ -16,6 +16,8 @@ @@ -16,6 +16,8 @@
package org.springframework.r2dbc.connection;
import java.util.Set;
import io.r2dbc.spi.Connection;
import io.r2dbc.spi.ConnectionFactory;
import io.r2dbc.spi.R2dbcBadGrammarException;
@ -69,6 +71,14 @@ public abstract class ConnectionFactoryUtils { @@ -69,6 +71,14 @@ public abstract class ConnectionFactoryUtils {
*/
public static final int CONNECTION_SYNCHRONIZATION_ORDER = 1000;
private static final Set<Integer> DUPLICATE_KEY_ERROR_CODES = Set.of(
1, // Oracle
301, // Sap Hana
1062, // MySQL/MariaDB
2601, // MS SQL Server
2627 // MS SQL Server
);
/**
* Obtain a {@link Connection} from the given {@link ConnectionFactory}.
@ -255,8 +265,7 @@ public abstract class ConnectionFactoryUtils { @@ -255,8 +265,7 @@ public abstract class ConnectionFactoryUtils {
*/
static boolean indicatesDuplicateKey(@Nullable String sqlState, int errorCode) {
return ("23505".equals(sqlState) ||
("23000".equals(sqlState) &&
(errorCode == 1 || errorCode == 1062 || errorCode == 2601 || errorCode == 2627)));
("23000".equals(sqlState) && DUPLICATE_KEY_ERROR_CODES.contains(errorCode)));
}
/**

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

@ -100,6 +100,10 @@ public class ConnectionFactoryUtilsUnitTests { @@ -100,6 +100,10 @@ public class ConnectionFactoryUtilsUnitTests {
new R2dbcDataIntegrityViolationException("reason", "23000", 1));
assertThat(exception).isExactlyInstanceOf(DuplicateKeyException.class);
exception = ConnectionFactoryUtils.convertR2dbcException("", "",
new R2dbcDataIntegrityViolationException("reason", "23000", 301));
assertThat(exception).isExactlyInstanceOf(DuplicateKeyException.class);
exception = ConnectionFactoryUtils.convertR2dbcException("", "",
new R2dbcDataIntegrityViolationException("reason", "23000", 1062));
assertThat(exception).isExactlyInstanceOf(DuplicateKeyException.class);

Loading…
Cancel
Save