Browse Source

Merge branch '6.0.x'

pull/31571/head
Sam Brannen 1 year ago
parent
commit
9759bf5964
  1. 8
      spring-core/src/test/java/org/springframework/core/AttributeAccessorSupportTests.java
  2. 10
      spring-jdbc/src/main/java/org/springframework/jdbc/support/SQLStateSQLExceptionTranslator.java
  3. 146
      spring-jdbc/src/test/java/org/springframework/jdbc/support/SQLErrorCodesFactoryTests.java
  4. 84
      spring-jdbc/src/test/java/org/springframework/jdbc/support/SQLStateSQLExceptionTranslatorTests.java
  5. 16
      spring-r2dbc/src/main/java/org/springframework/r2dbc/connection/ConnectionFactoryUtils.java
  6. 34
      spring-r2dbc/src/test/java/org/springframework/r2dbc/connection/ConnectionFactoryUtilsUnitTests.java

8
spring-core/src/test/java/org/springframework/core/AttributeAccessorSupportTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 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.
@ -16,7 +16,6 @@ @@ -16,7 +16,6 @@
package org.springframework.core;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
@ -82,10 +81,7 @@ class AttributeAccessorSupportTests { @@ -82,10 +81,7 @@ class AttributeAccessorSupportTests {
void attributeNames() {
this.attributeAccessor.setAttribute(NAME, VALUE);
this.attributeAccessor.setAttribute("abc", "123");
String[] attributeNames = this.attributeAccessor.attributeNames();
Arrays.sort(attributeNames);
assertThat(Arrays.binarySearch(attributeNames, "abc")).isEqualTo(0);
assertThat(Arrays.binarySearch(attributeNames, NAME)).isEqualTo(1);
assertThat(this.attributeAccessor.attributeNames()).contains("abc", NAME);
}
@SuppressWarnings("serial")

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

@ -90,8 +90,8 @@ public class SQLStateSQLExceptionTranslator extends AbstractFallbackSQLException @@ -90,8 +90,8 @@ public class SQLStateSQLExceptionTranslator extends AbstractFallbackSQLException
);
private static final Set<Integer> DUPLICATE_KEY_ERROR_CODES = Set.of(
1, // Oracle
301, // Sap Hana
1, // Oracle
301, // SAP HANA
1062, // MySQL/MariaDB
2601, // MS SQL Server
2627 // MS SQL Server
@ -163,12 +163,12 @@ public class SQLStateSQLExceptionTranslator extends AbstractFallbackSQLException @@ -163,12 +163,12 @@ public class SQLStateSQLExceptionTranslator extends AbstractFallbackSQLException
/**
* Check whether the given SQL state (and the associated error code in case
* 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.
* 23000 with a well-known vendor code.
* @param sqlState the SQL state value
* @param errorCode the error code value
* @param errorCode the error code
*/
static boolean indicatesDuplicateKey(@Nullable String sqlState, int errorCode) {
return ("23505".equals(sqlState) ||

146
spring-jdbc/src/test/java/org/springframework/jdbc/support/SQLErrorCodesFactoryTests.java

@ -19,7 +19,6 @@ package org.springframework.jdbc.support; @@ -19,7 +19,6 @@ package org.springframework.jdbc.support;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.Arrays;
import javax.sql.DataSource;
@ -41,108 +40,108 @@ import static org.mockito.Mockito.verify; @@ -41,108 +40,108 @@ import static org.mockito.Mockito.verify;
* @author Thomas Risberg
* @author Stephane Nicoll
* @author Juergen Hoeller
* @author Sam Brannen
*/
public class SQLErrorCodesFactoryTests {
class SQLErrorCodesFactoryTests {
/**
* Check that a default instance returns empty error codes for an unknown database.
*/
@Test
public void testDefaultInstanceWithNoSuchDatabase() {
void defaultInstanceWithNoSuchDatabase() {
SQLErrorCodes sec = SQLErrorCodesFactory.getInstance().getErrorCodes("xx");
assertThat(sec.getBadSqlGrammarCodes().length).isEqualTo(0);
assertThat(sec.getDataIntegrityViolationCodes().length).isEqualTo(0);
assertThat(sec.getBadSqlGrammarCodes()).isEmpty();
assertThat(sec.getDataIntegrityViolationCodes()).isEmpty();
}
/**
* Check that a known database produces recognizable codes.
*/
@Test
public void testDefaultInstanceWithOracle() {
void defaultInstanceWithOracle() {
SQLErrorCodes sec = SQLErrorCodesFactory.getInstance().getErrorCodes("Oracle");
assertIsOracle(sec);
}
private void assertIsOracle(SQLErrorCodes sec) {
assertThat(sec.getBadSqlGrammarCodes().length).isGreaterThan(0);
assertThat(sec.getDataIntegrityViolationCodes().length).isGreaterThan(0);
assertThat(sec.getBadSqlGrammarCodes()).isNotEmpty();
assertThat(sec.getDataIntegrityViolationCodes()).isNotEmpty();
// These had better be a Bad SQL Grammar code
assertThat(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "942")).isGreaterThanOrEqualTo(0);
assertThat(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "6550")).isGreaterThanOrEqualTo(0);
assertThat(sec.getBadSqlGrammarCodes()).contains("942");
assertThat(sec.getBadSqlGrammarCodes()).contains("6550");
// This had better NOT be
assertThat(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "9xx42")).isLessThan(0);
assertThat(sec.getBadSqlGrammarCodes()).doesNotContain("9xx42");
}
private void assertIsSQLServer(SQLErrorCodes sec) {
assertThat(sec.getDatabaseProductName()).isEqualTo("Microsoft SQL Server");
assertThat(sec.getBadSqlGrammarCodes().length).isGreaterThan(0);
assertThat(sec.getBadSqlGrammarCodes()).isNotEmpty();
assertThat(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "156")).isGreaterThanOrEqualTo(0);
assertThat(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "170")).isGreaterThanOrEqualTo(0);
assertThat(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "207")).isGreaterThanOrEqualTo(0);
assertThat(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "208")).isGreaterThanOrEqualTo(0);
assertThat(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "209")).isGreaterThanOrEqualTo(0);
assertThat(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "9xx42")).isLessThan(0);
assertThat(sec.getBadSqlGrammarCodes()).contains("156");
assertThat(sec.getBadSqlGrammarCodes()).contains("170");
assertThat(sec.getBadSqlGrammarCodes()).contains("207");
assertThat(sec.getBadSqlGrammarCodes()).contains("208");
assertThat(sec.getBadSqlGrammarCodes()).contains("209");
assertThat(sec.getBadSqlGrammarCodes()).doesNotContain("9xx42");
assertThat(sec.getPermissionDeniedCodes().length).isGreaterThan(0);
assertThat(Arrays.binarySearch(sec.getPermissionDeniedCodes(), "229")).isGreaterThanOrEqualTo(0);
assertThat(sec.getPermissionDeniedCodes()).isNotEmpty();
assertThat(sec.getPermissionDeniedCodes()).contains("229");
assertThat(sec.getDuplicateKeyCodes().length).isGreaterThan(0);
assertThat(Arrays.binarySearch(sec.getDuplicateKeyCodes(), "2601")).isGreaterThanOrEqualTo(0);
assertThat(Arrays.binarySearch(sec.getDuplicateKeyCodes(), "2627")).isGreaterThanOrEqualTo(0);
assertThat(sec.getDuplicateKeyCodes()).isNotEmpty();
assertThat(sec.getDuplicateKeyCodes()).contains("2601");
assertThat(sec.getDuplicateKeyCodes()).contains("2627");
assertThat(sec.getDataIntegrityViolationCodes().length).isGreaterThan(0);
assertThat(Arrays.binarySearch(sec.getDataIntegrityViolationCodes(), "544")).isGreaterThanOrEqualTo(0);
assertThat(Arrays.binarySearch(sec.getDataIntegrityViolationCodes(), "8114")).isGreaterThanOrEqualTo(0);
assertThat(Arrays.binarySearch(sec.getDataIntegrityViolationCodes(), "8115")).isGreaterThanOrEqualTo(0);
assertThat(sec.getDataIntegrityViolationCodes()).isNotEmpty();
assertThat(sec.getDataIntegrityViolationCodes()).contains("544");
assertThat(sec.getDataIntegrityViolationCodes()).contains("8114");
assertThat(sec.getDataIntegrityViolationCodes()).contains("8115");
assertThat(sec.getDataAccessResourceFailureCodes().length).isGreaterThan(0);
assertThat(Arrays.binarySearch(sec.getDataAccessResourceFailureCodes(), "4060")).isGreaterThanOrEqualTo(0);
assertThat(sec.getDataAccessResourceFailureCodes()).isNotEmpty();
assertThat(sec.getDataAccessResourceFailureCodes()).contains("4060");
assertThat(sec.getCannotAcquireLockCodes().length).isGreaterThan(0);
assertThat(Arrays.binarySearch(sec.getCannotAcquireLockCodes(), "1222")).isGreaterThanOrEqualTo(0);
assertThat(sec.getCannotAcquireLockCodes()).isNotEmpty();
assertThat(sec.getCannotAcquireLockCodes()).contains("1222");
assertThat(sec.getDeadlockLoserCodes().length).isGreaterThan(0);
assertThat(Arrays.binarySearch(sec.getDeadlockLoserCodes(), "1205")).isGreaterThanOrEqualTo(0);
assertThat(sec.getDeadlockLoserCodes()).isNotEmpty();
assertThat(sec.getDeadlockLoserCodes()).contains("1205");
}
private void assertIsHsql(SQLErrorCodes sec) {
assertThat(sec.getBadSqlGrammarCodes().length).isGreaterThan(0);
assertThat(sec.getDataIntegrityViolationCodes().length).isGreaterThan(0);
assertThat(sec.getBadSqlGrammarCodes()).isNotEmpty();
assertThat(sec.getDataIntegrityViolationCodes()).isNotEmpty();
// This had better be a Bad SQL Grammar code
assertThat(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "-22")).isGreaterThanOrEqualTo(0);
assertThat(sec.getBadSqlGrammarCodes()).contains("-22");
// This had better NOT be
assertThat(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "-9")).isLessThan(0);
assertThat(sec.getBadSqlGrammarCodes()).doesNotContain("-9");
}
private void assertIsDB2(SQLErrorCodes sec) {
assertThat(sec.getBadSqlGrammarCodes().length).isGreaterThan(0);
assertThat(sec.getDataIntegrityViolationCodes().length).isGreaterThan(0);
assertThat(sec.getBadSqlGrammarCodes()).isNotEmpty();
assertThat(sec.getDataIntegrityViolationCodes()).isNotEmpty();
assertThat(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "942")).isLessThan(0);
assertThat(sec.getBadSqlGrammarCodes()).doesNotContain("942");
// This had better NOT be
assertThat(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "-204")).isGreaterThanOrEqualTo(0);
assertThat(sec.getBadSqlGrammarCodes()).contains("-204");
}
private void assertIsHana(SQLErrorCodes sec) {
assertThat(sec.getBadSqlGrammarCodes().length).isGreaterThan(0);
assertThat(sec.getDataIntegrityViolationCodes().length).isGreaterThan(0);
assertThat(Arrays.binarySearch(sec.getBadSqlGrammarCodes(), "368")).isGreaterThanOrEqualTo(0);
assertThat(Arrays.binarySearch(sec.getPermissionDeniedCodes(), "10")).isGreaterThanOrEqualTo(0);
assertThat(Arrays.binarySearch(sec.getDuplicateKeyCodes(), "301")).isGreaterThanOrEqualTo(0);
assertThat(Arrays.binarySearch(sec.getDataIntegrityViolationCodes(), "461")).isGreaterThanOrEqualTo(0);
assertThat(Arrays.binarySearch(sec.getDataAccessResourceFailureCodes(), "-813")).isGreaterThanOrEqualTo(0);
assertThat(Arrays.binarySearch(sec.getInvalidResultSetAccessCodes(), "582")).isGreaterThanOrEqualTo(0);
assertThat(Arrays.binarySearch(sec.getCannotAcquireLockCodes(), "131")).isGreaterThanOrEqualTo(0);
assertThat(Arrays.binarySearch(sec.getCannotSerializeTransactionCodes(), "138")).isGreaterThanOrEqualTo(0);
assertThat(Arrays.binarySearch(sec.getDeadlockLoserCodes(), "133")).isGreaterThanOrEqualTo(0);
assertThat(sec.getBadSqlGrammarCodes()).isNotEmpty();
assertThat(sec.getDataIntegrityViolationCodes()).isNotEmpty();
assertThat(sec.getBadSqlGrammarCodes()).contains("368");
assertThat(sec.getPermissionDeniedCodes()).contains("10");
assertThat(sec.getDuplicateKeyCodes()).contains("301");
assertThat(sec.getDataIntegrityViolationCodes()).contains("461");
assertThat(sec.getDataAccessResourceFailureCodes()).contains("-813");
assertThat(sec.getInvalidResultSetAccessCodes()).contains("582");
assertThat(sec.getCannotAcquireLockCodes()).contains("131");
assertThat(sec.getCannotSerializeTransactionCodes()).contains("138");
assertThat(sec.getDeadlockLoserCodes()).contains("133");
}
@Test
public void testLookupOrder() {
void lookupOrder() {
class TestSQLErrorCodesFactory extends SQLErrorCodesFactory {
private int lookups = 0;
@Override
@ -163,15 +162,15 @@ public class SQLErrorCodesFactoryTests { @@ -163,15 +162,15 @@ public class SQLErrorCodesFactoryTests {
// Should have failed to load without error
TestSQLErrorCodesFactory sf = new TestSQLErrorCodesFactory();
assertThat(sf.getErrorCodes("XX").getBadSqlGrammarCodes().length).isEqualTo(0);
assertThat(sf.getErrorCodes("Oracle").getDataIntegrityViolationCodes().length).isEqualTo(0);
assertThat(sf.getErrorCodes("XX").getBadSqlGrammarCodes()).isEmpty();
assertThat(sf.getErrorCodes("Oracle").getDataIntegrityViolationCodes()).isEmpty();
}
/**
* Check that user defined error codes take precedence.
*/
@Test
public void testFindUserDefinedCodes() {
void findUserDefinedCodes() {
class TestSQLErrorCodesFactory extends SQLErrorCodesFactory {
@Override
protected Resource loadResource(String path) {
@ -184,14 +183,12 @@ public class SQLErrorCodesFactoryTests { @@ -184,14 +183,12 @@ public class SQLErrorCodesFactoryTests {
// Should have loaded without error
TestSQLErrorCodesFactory sf = new TestSQLErrorCodesFactory();
assertThat(sf.getErrorCodes("XX").getBadSqlGrammarCodes().length).isEqualTo(0);
assertThat(sf.getErrorCodes("Oracle").getBadSqlGrammarCodes()).hasSize(2);
assertThat(sf.getErrorCodes("Oracle").getBadSqlGrammarCodes()[0]).isEqualTo("1");
assertThat(sf.getErrorCodes("Oracle").getBadSqlGrammarCodes()[1]).isEqualTo("2");
assertThat(sf.getErrorCodes("XX").getBadSqlGrammarCodes()).isEmpty();
assertThat(sf.getErrorCodes("Oracle").getBadSqlGrammarCodes()).containsExactly("1", "2");
}
@Test
public void testInvalidUserDefinedCodeFormat() {
void invalidUserDefinedCodeFormat() {
class TestSQLErrorCodesFactory extends SQLErrorCodesFactory {
@Override
protected Resource loadResource(String path) {
@ -205,7 +202,7 @@ public class SQLErrorCodesFactoryTests { @@ -205,7 +202,7 @@ public class SQLErrorCodesFactoryTests {
// Should have failed to load without error
TestSQLErrorCodesFactory sf = new TestSQLErrorCodesFactory();
assertThat(sf.getErrorCodes("XX").getBadSqlGrammarCodes().length).isEqualTo(0);
assertThat(sf.getErrorCodes("XX").getBadSqlGrammarCodes()).isEmpty();
assertThat(sf.getErrorCodes("Oracle").getBadSqlGrammarCodes()).isEmpty();
}
@ -213,7 +210,7 @@ public class SQLErrorCodesFactoryTests { @@ -213,7 +210,7 @@ public class SQLErrorCodesFactoryTests {
* Check that custom error codes take precedence.
*/
@Test
public void testFindCustomCodes() {
void findCustomCodes() {
class TestSQLErrorCodesFactory extends SQLErrorCodesFactory {
@Override
protected Resource loadResource(String path) {
@ -227,14 +224,13 @@ public class SQLErrorCodesFactoryTests { @@ -227,14 +224,13 @@ public class SQLErrorCodesFactoryTests {
// Should have loaded without error
TestSQLErrorCodesFactory sf = new TestSQLErrorCodesFactory();
assertThat(sf.getErrorCodes("Oracle").getCustomTranslations()).hasSize(1);
CustomSQLErrorCodesTranslation translation =
sf.getErrorCodes("Oracle").getCustomTranslations()[0];
CustomSQLErrorCodesTranslation translation = sf.getErrorCodes("Oracle").getCustomTranslations()[0];
assertThat(translation.getExceptionClass()).isEqualTo(CustomErrorCodeException.class);
assertThat(translation.getErrorCodes()).hasSize(1);
}
@Test
public void testDataSourceWithNullMetadata() throws Exception {
void dataSourceWithNullMetadata() throws Exception {
Connection connection = mock();
DataSource dataSource = mock();
given(dataSource.getConnection()).willReturn(connection);
@ -250,7 +246,7 @@ public class SQLErrorCodesFactoryTests { @@ -250,7 +246,7 @@ public class SQLErrorCodesFactoryTests {
}
@Test
public void testGetFromDataSourceWithSQLException() throws Exception {
void getFromDataSourceWithSQLException() throws Exception {
SQLException expectedSQLException = new SQLException();
DataSource dataSource = mock();
@ -284,25 +280,25 @@ public class SQLErrorCodesFactoryTests { @@ -284,25 +280,25 @@ public class SQLErrorCodesFactoryTests {
}
@Test
public void testSQLServerRecognizedFromMetadata() throws Exception {
void sqlServerRecognizedFromMetadata() throws Exception {
SQLErrorCodes sec = getErrorCodesFromDataSource("MS-SQL", null);
assertIsSQLServer(sec);
}
@Test
public void testOracleRecognizedFromMetadata() throws Exception {
void oracleRecognizedFromMetadata() throws Exception {
SQLErrorCodes sec = getErrorCodesFromDataSource("Oracle", null);
assertIsOracle(sec);
}
@Test
public void testHsqlRecognizedFromMetadata() throws Exception {
void hsqlRecognizedFromMetadata() throws Exception {
SQLErrorCodes sec = getErrorCodesFromDataSource("HSQL Database Engine", null);
assertIsHsql(sec);
}
@Test
public void testDB2RecognizedFromMetadata() throws Exception {
void dB2RecognizedFromMetadata() throws Exception {
SQLErrorCodes sec = getErrorCodesFromDataSource("DB2", null);
assertIsDB2(sec);
sec = getErrorCodesFromDataSource("DB2/", null);
@ -312,7 +308,7 @@ public class SQLErrorCodesFactoryTests { @@ -312,7 +308,7 @@ public class SQLErrorCodesFactoryTests {
}
@Test
public void testHanaIsRecognizedFromMetadata() throws Exception {
void hanaIsRecognizedFromMetadata() throws Exception {
SQLErrorCodes sec = getErrorCodesFromDataSource("SAP DB", null);
assertIsHana(sec);
}
@ -321,7 +317,7 @@ public class SQLErrorCodesFactoryTests { @@ -321,7 +317,7 @@ public class SQLErrorCodesFactoryTests {
* Check that wild card database name works.
*/
@Test
public void testWildCardNameRecognized() throws Exception {
void wildCardNameRecognized() throws Exception {
class WildcardSQLErrorCodesFactory extends SQLErrorCodesFactory {
@Override
protected Resource loadResource(String path) {

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

@ -34,86 +34,89 @@ import static org.assertj.core.api.Assertions.assertThat; @@ -34,86 +34,89 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
/**
* Tests for {@link SQLStateSQLExceptionTranslator}.
*
* @author Rick Evans
* @author Juergen Hoeller
* @author Chris Beams
*/
public class SQLStateSQLExceptionTranslatorTests {
class SQLStateSQLExceptionTranslatorTests {
private final SQLExceptionTranslator translator = new SQLStateSQLExceptionTranslator();
@Test
public void translateNullException() {
assertThatIllegalArgumentException().isThrownBy(() ->
new SQLStateSQLExceptionTranslator().translate("", "", null));
void translateNullException() {
assertThatIllegalArgumentException().isThrownBy(() -> translator.translate("", "", null));
}
@Test
public void translateBadSqlGrammar() {
doTest("07", BadSqlGrammarException.class);
void translateBadSqlGrammar() {
assertTranslation("07", BadSqlGrammarException.class);
}
@Test
public void translateDataIntegrityViolation() {
doTest("23", DataIntegrityViolationException.class);
void translateDataIntegrityViolation() {
assertTranslation("23", DataIntegrityViolationException.class);
}
@Test
public void translateDuplicateKey() {
doTest("23505", DuplicateKeyException.class);
void translateDuplicateKey() {
assertTranslation("23505", DuplicateKeyException.class);
}
@Test
public void translateDuplicateKeyOracle() {
doTest("23000", 1, DuplicateKeyException.class);
void translateDuplicateKeyOracle() {
assertTranslation("23000", 1, DuplicateKeyException.class);
}
@Test
public void translateDuplicateKeyMySQL() {
doTest("23000", 1062, DuplicateKeyException.class);
void translateDuplicateKeyMySQL() {
assertTranslation("23000", 1062, DuplicateKeyException.class);
}
@Test
public void translateDuplicateKeyMSSQL1() {
doTest("23000", 2601, DuplicateKeyException.class);
void translateDuplicateKeyMSSQL1() {
assertTranslation("23000", 2601, DuplicateKeyException.class);
}
@Test
public void translateDuplicateKeyMSSQL2() {
doTest("23000", 2627, DuplicateKeyException.class);
void translateDuplicateKeyMSSQL2() {
assertTranslation("23000", 2627, DuplicateKeyException.class);
}
@Test
public void translateDuplicateKeySapHana() {
doTest("23000", 301, DuplicateKeyException.class);
@Test // gh-31554
void translateDuplicateKeySapHana() {
assertTranslation("23000", 301, DuplicateKeyException.class);
}
@Test
public void translateDataAccessResourceFailure() {
doTest("53", DataAccessResourceFailureException.class);
void translateDataAccessResourceFailure() {
assertTranslation("53", DataAccessResourceFailureException.class);
}
@Test
public void translateTransientDataAccessResourceFailure() {
doTest("S1", TransientDataAccessResourceException.class);
void translateTransientDataAccessResourceFailure() {
assertTranslation("S1", TransientDataAccessResourceException.class);
}
@Test
public void translatePessimisticLockingFailure() {
doTest("40", PessimisticLockingFailureException.class);
void translatePessimisticLockingFailure() {
assertTranslation("40", PessimisticLockingFailureException.class);
}
@Test
public void translateCannotAcquireLock() {
doTest("40001", CannotAcquireLockException.class);
void translateCannotAcquireLock() {
assertTranslation("40001", CannotAcquireLockException.class);
}
@Test
public void translateUncategorized() {
doTest("00000000", null);
void translateUncategorized() {
assertTranslation("00000000", null);
}
@Test
public void invalidSqlStateCode() {
doTest("NO SUCH CODE", null);
void invalidSqlStateCode() {
assertTranslation("NO SUCH CODE", null);
}
/**
@ -122,19 +125,18 @@ public class SQLStateSQLExceptionTranslatorTests { @@ -122,19 +125,18 @@ public class SQLStateSQLExceptionTranslatorTests {
* Bug 729170
*/
@Test
public void malformedSqlStateCodes() {
doTest(null, null);
doTest("", null);
doTest("I", null);
void malformedSqlStateCodes() {
assertTranslation(null, null);
assertTranslation("", null);
assertTranslation("I", null);
}
private void doTest(@Nullable String sqlState, @Nullable Class<?> dataAccessExceptionType) {
doTest(sqlState, 0, dataAccessExceptionType);
private void assertTranslation(@Nullable String sqlState, @Nullable Class<?> dataAccessExceptionType) {
assertTranslation(sqlState, 0, dataAccessExceptionType);
}
private void doTest(@Nullable String sqlState, int errorCode, @Nullable Class<?> dataAccessExceptionType) {
SQLExceptionTranslator translator = new SQLStateSQLExceptionTranslator();
private void assertTranslation(@Nullable String sqlState, int errorCode, @Nullable Class<?> dataAccessExceptionType) {
SQLException ex = new SQLException("reason", sqlState, errorCode);
DataAccessException dax = translator.translate("task", "SQL", ex);

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

@ -72,12 +72,12 @@ public abstract class ConnectionFactoryUtils { @@ -72,12 +72,12 @@ 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
1, // Oracle
301, // SAP HANA
1062, // MySQL/MariaDB
2601, // MS SQL Server
2627 // MS SQL Server
);
);
/**
@ -257,11 +257,13 @@ public abstract class ConnectionFactoryUtils { @@ -257,11 +257,13 @@ public abstract class ConnectionFactoryUtils {
}
/**
* Check whether the given SQL state (and the associated error code in case
* of a generic SQL state value) indicate a duplicate key exception. See
* {@code org.springframework.jdbc.support.SQLStateSQLExceptionTranslator#indicatesDuplicateKey}.
* Check whether the given SQL state and the associated error code (in case
* 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 a well-known vendor code.
* @param sqlState the SQL state value
* @param errorCode the error code value
* @param errorCode the error code
* @see org.springframework.jdbc.support.SQLStateSQLExceptionTranslator#indicatesDuplicateKey
*/
static boolean indicatesDuplicateKey(@Nullable String sqlState, int errorCode) {
return ("23505".equals(sqlState) ||

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

@ -45,17 +45,17 @@ import static org.assertj.core.api.Assertions.assertThat; @@ -45,17 +45,17 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Mark Paluch
* @author Juergen Hoeller
*/
public class ConnectionFactoryUtilsUnitTests {
class ConnectionFactoryUtilsUnitTests {
@Test
public void shouldTranslateTransientResourceException() {
void shouldTranslateTransientResourceException() {
Exception exception = ConnectionFactoryUtils.convertR2dbcException("", "",
new R2dbcTransientResourceException(""));
assertThat(exception).isExactlyInstanceOf(TransientDataAccessResourceException.class);
}
@Test
public void shouldTranslateRollbackException() {
void shouldTranslateRollbackException() {
Exception exception = ConnectionFactoryUtils.convertR2dbcException("", "",
new R2dbcRollbackException());
assertThat(exception).isExactlyInstanceOf(PessimisticLockingFailureException.class);
@ -66,28 +66,28 @@ public class ConnectionFactoryUtilsUnitTests { @@ -66,28 +66,28 @@ public class ConnectionFactoryUtilsUnitTests {
}
@Test
public void shouldTranslateTimeoutException() {
void shouldTranslateTimeoutException() {
Exception exception = ConnectionFactoryUtils.convertR2dbcException("", "",
new R2dbcTimeoutException());
assertThat(exception).isExactlyInstanceOf(QueryTimeoutException.class);
}
@Test
public void shouldNotTranslateUnknownExceptions() {
void shouldNotTranslateUnknownExceptions() {
Exception exception = ConnectionFactoryUtils.convertR2dbcException("", "",
new MyTransientException());
assertThat(exception).isExactlyInstanceOf(UncategorizedR2dbcException.class);
}
@Test
public void shouldTranslateNonTransientResourceException() {
void shouldTranslateNonTransientResourceException() {
Exception exception = ConnectionFactoryUtils.convertR2dbcException("", "",
new R2dbcNonTransientResourceException());
assertThat(exception).isExactlyInstanceOf(DataAccessResourceFailureException.class);
}
@Test
public void shouldTranslateIntegrityViolationException() {
void shouldTranslateIntegrityViolationException() {
Exception exception = ConnectionFactoryUtils.convertR2dbcException("", "",
new R2dbcDataIntegrityViolationException());
assertThat(exception).isExactlyInstanceOf(DataIntegrityViolationException.class);
@ -98,41 +98,41 @@ public class ConnectionFactoryUtilsUnitTests { @@ -98,41 +98,41 @@ public class ConnectionFactoryUtilsUnitTests {
exception = ConnectionFactoryUtils.convertR2dbcException("", "",
new R2dbcDataIntegrityViolationException("reason", "23000", 1));
assertThat(exception).isExactlyInstanceOf(DuplicateKeyException.class);
assertThat(exception).as("Oracle").isExactlyInstanceOf(DuplicateKeyException.class);
exception = ConnectionFactoryUtils.convertR2dbcException("", "",
new R2dbcDataIntegrityViolationException("reason", "23000", 301));
assertThat(exception).isExactlyInstanceOf(DuplicateKeyException.class);
assertThat(exception).as("SAP HANA").isExactlyInstanceOf(DuplicateKeyException.class);
exception = ConnectionFactoryUtils.convertR2dbcException("", "",
new R2dbcDataIntegrityViolationException("reason", "23000", 1062));
assertThat(exception).isExactlyInstanceOf(DuplicateKeyException.class);
assertThat(exception).as("MySQL/MariaDB").isExactlyInstanceOf(DuplicateKeyException.class);
exception = ConnectionFactoryUtils.convertR2dbcException("", "",
new R2dbcDataIntegrityViolationException("reason", "23000", 2601));
assertThat(exception).isExactlyInstanceOf(DuplicateKeyException.class);
assertThat(exception).as("MS SQL Server").isExactlyInstanceOf(DuplicateKeyException.class);
exception = ConnectionFactoryUtils.convertR2dbcException("", "",
new R2dbcDataIntegrityViolationException("reason", "23000", 2627));
assertThat(exception).isExactlyInstanceOf(DuplicateKeyException.class);
assertThat(exception).as("MS SQL Server").isExactlyInstanceOf(DuplicateKeyException.class);
}
@Test
public void shouldTranslatePermissionDeniedException() {
void shouldTranslatePermissionDeniedException() {
Exception exception = ConnectionFactoryUtils.convertR2dbcException("", "",
new R2dbcPermissionDeniedException());
assertThat(exception).isExactlyInstanceOf(PermissionDeniedDataAccessException.class);
}
@Test
public void shouldTranslateBadSqlGrammarException() {
void shouldTranslateBadSqlGrammarException() {
Exception exception = ConnectionFactoryUtils.convertR2dbcException("", "",
new R2dbcBadGrammarException());
assertThat(exception).isExactlyInstanceOf(BadSqlGrammarException.class);
}
@Test
public void messageGeneration() {
void messageGeneration() {
Exception exception = ConnectionFactoryUtils.convertR2dbcException("TASK",
"SOME-SQL", new R2dbcTransientResourceException("MESSAGE"));
assertThat(exception).isExactlyInstanceOf(
@ -140,7 +140,7 @@ public class ConnectionFactoryUtilsUnitTests { @@ -140,7 +140,7 @@ public class ConnectionFactoryUtilsUnitTests {
}
@Test
public void messageGenerationNullSQL() {
void messageGenerationNullSQL() {
Exception exception = ConnectionFactoryUtils.convertR2dbcException("TASK", null,
new R2dbcTransientResourceException("MESSAGE"));
assertThat(exception).isExactlyInstanceOf(
@ -148,7 +148,7 @@ public class ConnectionFactoryUtilsUnitTests { @@ -148,7 +148,7 @@ public class ConnectionFactoryUtilsUnitTests {
}
@Test
public void messageGenerationNullMessage() {
void messageGenerationNullMessage() {
Exception exception = ConnectionFactoryUtils.convertR2dbcException("TASK",
"SOME-SQL", new R2dbcTransientResourceException());
assertThat(exception).isExactlyInstanceOf(

Loading…
Cancel
Save