diff --git a/spring-jdbc/src/main/java/org/springframework/jdbc/support/SQLStateSQLExceptionTranslator.java b/spring-jdbc/src/main/java/org/springframework/jdbc/support/SQLStateSQLExceptionTranslator.java index 18e816f5def..f8d64b94859 100644 --- a/spring-jdbc/src/main/java/org/springframework/jdbc/support/SQLStateSQLExceptionTranslator.java +++ b/spring-jdbc/src/main/java/org/springframework/jdbc/support/SQLStateSQLExceptionTranslator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -153,14 +153,14 @@ public class SQLStateSQLExceptionTranslator extends AbstractFallbackSQLException * of a generic SQL state value) indicate a duplicate key exception: * 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, - * 2627 for MS SQL Server). + * 2601/2627 for MS SQL Server). * @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 == 2627))); + (errorCode == 1 || errorCode == 1062 || errorCode == 2601 || errorCode == 2627))); } } diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/support/SQLExceptionSubclassTranslatorTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/support/SQLExceptionSubclassTranslatorTests.java index 15097444ac0..ce258043113 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/support/SQLExceptionSubclassTranslatorTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/support/SQLExceptionSubclassTranslatorTests.java @@ -59,6 +59,7 @@ public class SQLExceptionSubclassTranslatorTests { doTest(new SQLIntegrityConstraintViolationException("", "23505", 0), DuplicateKeyException.class); doTest(new SQLIntegrityConstraintViolationException("", "23000", 1), DuplicateKeyException.class); doTest(new SQLIntegrityConstraintViolationException("", "23000", 1062), DuplicateKeyException.class); + doTest(new SQLIntegrityConstraintViolationException("", "23000", 2601), DuplicateKeyException.class); doTest(new SQLIntegrityConstraintViolationException("", "23000", 2627), DuplicateKeyException.class); doTest(new SQLInvalidAuthorizationSpecException("", "", 0), PermissionDeniedDataAccessException.class); doTest(new SQLNonTransientConnectionException("", "", 0), DataAccessResourceFailureException.class); diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/support/SQLStateSQLExceptionTranslatorTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/support/SQLStateSQLExceptionTranslatorTests.java index 1d4c3b24025..f9e0a492f4a 100644 --- a/spring-jdbc/src/test/java/org/springframework/jdbc/support/SQLStateSQLExceptionTranslatorTests.java +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/support/SQLStateSQLExceptionTranslatorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -72,7 +72,12 @@ public class SQLStateSQLExceptionTranslatorTests { } @Test - public void translateDuplicateKeyMSSQL() { + public void translateDuplicateKeyMSSQL1() { + doTest("23000", 2601, DuplicateKeyException.class); + } + + @Test + public void translateDuplicateKeyMSSQL2() { doTest("23000", 2627, DuplicateKeyException.class); } diff --git a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/ConnectionFactoryUtils.java b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/ConnectionFactoryUtils.java index e34c6db1c00..e7638eda114 100644 --- a/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/ConnectionFactoryUtils.java +++ b/spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/ConnectionFactoryUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2022 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -256,7 +256,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 == 2627))); + (errorCode == 1 || errorCode == 1062 || errorCode == 2601 || errorCode == 2627))); } /** diff --git a/spring-r2dbc/src/test/java/org/springframework/r2dbc/connection/ConnectionFactoryUtilsUnitTests.java b/spring-r2dbc/src/test/java/org/springframework/r2dbc/connection/ConnectionFactoryUtilsUnitTests.java index caa3b6d4e20..3362e0c1fe0 100644 --- a/spring-r2dbc/src/test/java/org/springframework/r2dbc/connection/ConnectionFactoryUtilsUnitTests.java +++ b/spring-r2dbc/src/test/java/org/springframework/r2dbc/connection/ConnectionFactoryUtilsUnitTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2019-2022 the original author or authors. + * Copyright 2019-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -104,6 +104,10 @@ public class ConnectionFactoryUtilsUnitTests { new R2dbcDataIntegrityViolationException("reason", "23000", 1062)); assertThat(exception).isExactlyInstanceOf(DuplicateKeyException.class); + exception = ConnectionFactoryUtils.convertR2dbcException("", "", + new R2dbcDataIntegrityViolationException("reason", "23000", 2601)); + assertThat(exception).isExactlyInstanceOf(DuplicateKeyException.class); + exception = ConnectionFactoryUtils.convertR2dbcException("", "", new R2dbcDataIntegrityViolationException("reason", "23000", 2627)); assertThat(exception).isExactlyInstanceOf(DuplicateKeyException.class);