Merge pull request #44470 from quaff

* pr/44470:
  Polish "Add additional configuration properties for JdbcTemplate"
  Add additional configuration properties for JdbcTemplate.

Closes gh-44470
This commit is contained in:
Stéphane Nicoll 2025-03-07 10:40:56 +01:00
commit ef3eaf3e86
3 changed files with 73 additions and 2 deletions

View File

@ -43,6 +43,12 @@ public class JdbcProperties {
*/
public static class Template {
/**
* Whether to ignore JDBC statement warnings (SQLWarning). When set to false,
* throw a SQLWarningException instead.
*/
private boolean ignoreWarnings = true;
/**
* Number of rows that should be fetched from the database when more rows are
* needed. Use -1 to use the JDBC driver's default configuration.
@ -61,6 +67,31 @@ public class JdbcProperties {
@DurationUnit(ChronoUnit.SECONDS)
private Duration queryTimeout;
/**
* Whether results processing should be skipped. Can be used to optimize callable
* statement processing when we know that no results are being passed back.
*/
private boolean skipResultsProcessing;
/**
* Whether undeclared results should be skipped.
*/
private boolean skipUndeclaredResults;
/**
* Whether execution of a CallableStatement will return the results in a Map that
* uses case-insensitive names for the parameters.
*/
private boolean resultsMapCaseInsensitive;
public boolean isIgnoreWarnings() {
return this.ignoreWarnings;
}
public void setIgnoreWarnings(boolean ignoreWarnings) {
this.ignoreWarnings = ignoreWarnings;
}
public int getFetchSize() {
return this.fetchSize;
}
@ -85,6 +116,30 @@ public class JdbcProperties {
this.queryTimeout = queryTimeout;
}
public boolean isSkipResultsProcessing() {
return this.skipResultsProcessing;
}
public void setSkipResultsProcessing(boolean skipResultsProcessing) {
this.skipResultsProcessing = skipResultsProcessing;
}
public boolean isSkipUndeclaredResults() {
return this.skipUndeclaredResults;
}
public void setSkipUndeclaredResults(boolean skipUndeclaredResults) {
this.skipUndeclaredResults = skipUndeclaredResults;
}
public boolean isResultsMapCaseInsensitive() {
return this.resultsMapCaseInsensitive;
}
public void setResultsMapCaseInsensitive(boolean resultsMapCaseInsensitive) {
this.resultsMapCaseInsensitive = resultsMapCaseInsensitive;
}
}
}

View File

@ -31,6 +31,7 @@ import org.springframework.jdbc.support.SQLExceptionTranslator;
* Configuration for {@link JdbcTemplateConfiguration}.
*
* @author Stephane Nicoll
* @author Yanming Zhou
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnMissingBean(JdbcOperations.class)
@ -42,11 +43,15 @@ class JdbcTemplateConfiguration {
ObjectProvider<SQLExceptionTranslator> sqlExceptionTranslator) {
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
JdbcProperties.Template template = properties.getTemplate();
jdbcTemplate.setIgnoreWarnings(template.isIgnoreWarnings());
jdbcTemplate.setFetchSize(template.getFetchSize());
jdbcTemplate.setMaxRows(template.getMaxRows());
if (template.getQueryTimeout() != null) {
jdbcTemplate.setQueryTimeout((int) template.getQueryTimeout().getSeconds());
}
jdbcTemplate.setSkipResultsProcessing(template.isSkipResultsProcessing());
jdbcTemplate.setSkipUndeclaredResults(template.isSkipUndeclaredResults());
jdbcTemplate.setResultsMapCaseInsensitive(template.isResultsMapCaseInsensitive());
sqlExceptionTranslator.ifUnique(jdbcTemplate::setExceptionTranslator);
return jdbcTemplate;
}

View File

@ -66,24 +66,35 @@ class JdbcTemplateAutoConfigurationTests {
assertThat(context).hasSingleBean(JdbcOperations.class);
JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);
assertThat(jdbcTemplate.getDataSource()).isEqualTo(context.getBean(DataSource.class));
assertThat(jdbcTemplate.isIgnoreWarnings()).isEqualTo(true);
assertThat(jdbcTemplate.getFetchSize()).isEqualTo(-1);
assertThat(jdbcTemplate.getQueryTimeout()).isEqualTo(-1);
assertThat(jdbcTemplate.getMaxRows()).isEqualTo(-1);
assertThat(jdbcTemplate.isSkipResultsProcessing()).isEqualTo(false);
assertThat(jdbcTemplate.isSkipUndeclaredResults()).isEqualTo(false);
assertThat(jdbcTemplate.isResultsMapCaseInsensitive()).isEqualTo(false);
});
}
@Test
void testJdbcTemplateWithCustomProperties() {
this.contextRunner
.withPropertyValues("spring.jdbc.template.fetch-size:100", "spring.jdbc.template.query-timeout:60",
"spring.jdbc.template.max-rows:1000")
.withPropertyValues("spring.jdbc.template.ignore-warnings:false", "spring.jdbc.template.fetch-size:100",
"spring.jdbc.template.query-timeout:60", "spring.jdbc.template.max-rows:1000",
"spring.jdbc.template.skip-results-processing:true",
"spring.jdbc.template.skip-undeclared-results:true",
"spring.jdbc.template.results-map-case-insensitive:true")
.run((context) -> {
assertThat(context).hasSingleBean(JdbcOperations.class);
JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);
assertThat(jdbcTemplate.getDataSource()).isNotNull();
assertThat(jdbcTemplate.isIgnoreWarnings()).isEqualTo(false);
assertThat(jdbcTemplate.getFetchSize()).isEqualTo(100);
assertThat(jdbcTemplate.getQueryTimeout()).isEqualTo(60);
assertThat(jdbcTemplate.getMaxRows()).isEqualTo(1000);
assertThat(jdbcTemplate.isSkipResultsProcessing()).isEqualTo(true);
assertThat(jdbcTemplate.isSkipUndeclaredResults()).isEqualTo(true);
assertThat(jdbcTemplate.isResultsMapCaseInsensitive()).isEqualTo(true);
});
}