Use SQLExceptionSubclassTranslator by default (avoiding sql-error-codes.xml)

SQLErrorCodeSQLExceptionTranslator kicks in for user-provided sql-error-codes.xml files. It will still pick up Spring's legacy default error code mappings as well but only when triggered by a (potentially empty) user-provided file in the root of the classpath.

Closes gh-28216
This commit is contained in:
Juergen Hoeller
2022-06-14 14:00:56 +02:00
parent 4e1b9f1492
commit 083113d8a4
5 changed files with 45 additions and 38 deletions

View File

@@ -944,6 +944,8 @@ public class JdbcTemplateTests {
mockDatabaseMetaData(false);
given(this.connection.createStatement()).willReturn(this.preparedStatement);
this.template.setExceptionTranslator(new SQLErrorCodeSQLExceptionTranslator(this.dataSource));
assertThatExceptionOfType(BadSqlGrammarException.class).isThrownBy(() ->
this.template.query(sql, (RowCallbackHandler) rs -> {
throw sqlException;
@@ -955,20 +957,17 @@ public class JdbcTemplateTests {
}
@Test
public void testSQLErrorCodeTranslationWithSpecifiedDbName() throws Exception {
public void testSQLErrorCodeTranslationWithSpecifiedDatabaseName() throws Exception {
final SQLException sqlException = new SQLException("I have a known problem", "99999", 1054);
final String sql = "SELECT ID FROM CUSTOMER";
given(this.resultSet.next()).willReturn(true);
given(this.connection.createStatement()).willReturn(this.preparedStatement);
JdbcTemplate template = new JdbcTemplate();
template.setDataSource(this.dataSource);
template.setDatabaseProductName("MySQL");
template.afterPropertiesSet();
this.template.setExceptionTranslator(new SQLErrorCodeSQLExceptionTranslator("MySQL"));
assertThatExceptionOfType(BadSqlGrammarException.class).isThrownBy(() ->
template.query(sql, (RowCallbackHandler) rs -> {
this.template.query(sql, (RowCallbackHandler) rs -> {
throw sqlException;
}))
.withCause(sqlException);
@@ -983,7 +982,7 @@ public class JdbcTemplateTests {
* to get the metadata
*/
@Test
public void testUseCustomSQLErrorCodeTranslator() throws Exception {
public void testUseCustomExceptionTranslator() throws Exception {
// Bad SQL state
final SQLException sqlException = new SQLException("I have a known problem", "07000", 1054);
final String sql = "SELECT ID FROM CUSTOMER";