Skip to content

Commit db9e938

Browse files
sgfltsbrannen
authored andcommitted
Detect Informix error codes as DuplicateKeyException
Closes gh-35400 Signed-off-by: Lukáš Kvídera <[email protected]>
1 parent 3a4315b commit db9e938

File tree

4 files changed

+45
-28
lines changed

4 files changed

+45
-28
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ public class SQLStateSQLExceptionTranslator extends AbstractFallbackSQLException
9494
301, // SAP HANA
9595
1062, // MySQL/MariaDB
9696
2601, // MS SQL Server
97-
2627 // MS SQL Server
97+
2627, // MS SQL Server
98+
-239, // Informix
99+
-268 // Informix
98100
);
99101

100102

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ void translateDuplicateKeySapHana() {
9090
assertTranslation("23000", 301, DuplicateKeyException.class);
9191
}
9292

93+
@Test
94+
void translateDuplicateKeyInformix1() {
95+
assertTranslation("23000", -239, DuplicateKeyException.class);
96+
}
97+
98+
@Test
99+
void translateDuplicateKeyInformix2() {
100+
assertTranslation("23000", -268, DuplicateKeyException.class);
101+
}
102+
93103
@Test
94104
void translateDataAccessResourceFailure() {
95105
assertTranslation("53", DataAccessResourceFailureException.class);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ public abstract class ConnectionFactoryUtils {
7676
301, // SAP HANA
7777
1062, // MySQL/MariaDB
7878
2601, // MS SQL Server
79-
2627 // MS SQL Server
79+
2627, // MS SQL Server
80+
-239, // Informix
81+
-268 // Informix
8082
);
8183

8284

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

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
import io.r2dbc.spi.R2dbcTimeoutException;
2626
import io.r2dbc.spi.R2dbcTransientResourceException;
2727
import org.junit.jupiter.api.Test;
28-
28+
import org.junit.jupiter.params.ParameterizedTest;
29+
import org.junit.jupiter.params.provider.Arguments;
30+
import org.junit.jupiter.params.provider.MethodSource;
2931
import org.springframework.dao.CannotAcquireLockException;
3032
import org.springframework.dao.DataAccessResourceFailureException;
3133
import org.springframework.dao.DataIntegrityViolationException;
@@ -37,8 +39,12 @@
3739
import org.springframework.r2dbc.BadSqlGrammarException;
3840
import org.springframework.r2dbc.UncategorizedR2dbcException;
3941

42+
import java.util.stream.Stream;
43+
4044
import static org.assertj.core.api.Assertions.assertThat;
4145

46+
47+
4248
/**
4349
* Tests for {@link ConnectionFactoryUtils}.
4450
*
@@ -86,35 +92,32 @@ void shouldTranslateNonTransientResourceException() {
8692
assertThat(exception).isExactlyInstanceOf(DataAccessResourceFailureException.class);
8793
}
8894

95+
private static Stream<Arguments> duplicateKeyErrorCodes() {
96+
return Stream.of(
97+
Arguments.of("Oracle", "23505", 0),
98+
Arguments.of("Oracle", "23000", 1),
99+
Arguments.of("SAP HANA", "23000", 301),
100+
Arguments.of("MySQL/MariaDB", "23000", 1062),
101+
Arguments.of("MS SQL Server", "23000", 2601),
102+
Arguments.of("MS SQL Server", "23000", 2627),
103+
Arguments.of("Informix", "23000", -239),
104+
Arguments.of("Informix", "23000", -268)
105+
);
106+
}
107+
108+
@ParameterizedTest
109+
@MethodSource("duplicateKeyErrorCodes")
110+
void shouldTranslateIntegrityViolationException(final String db, String sqlState, final int errorCode) {
111+
Exception exception = ConnectionFactoryUtils.convertR2dbcException("", "",
112+
new R2dbcDataIntegrityViolationException("reason", sqlState, errorCode));
113+
assertThat(exception).as(db).isExactlyInstanceOf(DuplicateKeyException.class);
114+
}
115+
89116
@Test
90-
void shouldTranslateIntegrityViolationException() {
117+
void shouldTranslateGenericIntegrityViolationException() {
91118
Exception exception = ConnectionFactoryUtils.convertR2dbcException("", "",
92119
new R2dbcDataIntegrityViolationException());
93120
assertThat(exception).isExactlyInstanceOf(DataIntegrityViolationException.class);
94-
95-
exception = ConnectionFactoryUtils.convertR2dbcException("", "",
96-
new R2dbcDataIntegrityViolationException("reason", "23505"));
97-
assertThat(exception).isExactlyInstanceOf(DuplicateKeyException.class);
98-
99-
exception = ConnectionFactoryUtils.convertR2dbcException("", "",
100-
new R2dbcDataIntegrityViolationException("reason", "23000", 1));
101-
assertThat(exception).as("Oracle").isExactlyInstanceOf(DuplicateKeyException.class);
102-
103-
exception = ConnectionFactoryUtils.convertR2dbcException("", "",
104-
new R2dbcDataIntegrityViolationException("reason", "23000", 301));
105-
assertThat(exception).as("SAP HANA").isExactlyInstanceOf(DuplicateKeyException.class);
106-
107-
exception = ConnectionFactoryUtils.convertR2dbcException("", "",
108-
new R2dbcDataIntegrityViolationException("reason", "23000", 1062));
109-
assertThat(exception).as("MySQL/MariaDB").isExactlyInstanceOf(DuplicateKeyException.class);
110-
111-
exception = ConnectionFactoryUtils.convertR2dbcException("", "",
112-
new R2dbcDataIntegrityViolationException("reason", "23000", 2601));
113-
assertThat(exception).as("MS SQL Server").isExactlyInstanceOf(DuplicateKeyException.class);
114-
115-
exception = ConnectionFactoryUtils.convertR2dbcException("", "",
116-
new R2dbcDataIntegrityViolationException("reason", "23000", 2627));
117-
assertThat(exception).as("MS SQL Server").isExactlyInstanceOf(DuplicateKeyException.class);
118121
}
119122

120123
@Test

0 commit comments

Comments
 (0)