Skip to content

Commit ef3eaf3

Browse files
committed
Merge pull request #44470 from quaff
* pr/44470: Polish "Add additional configuration properties for JdbcTemplate" Add additional configuration properties for JdbcTemplate. Closes gh-44470
2 parents 6a5f8ac + 0e09a2d commit ef3eaf3

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcProperties.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ public Template getTemplate() {
4343
*/
4444
public static class Template {
4545

46+
/**
47+
* Whether to ignore JDBC statement warnings (SQLWarning). When set to false,
48+
* throw a SQLWarningException instead.
49+
*/
50+
private boolean ignoreWarnings = true;
51+
4652
/**
4753
* Number of rows that should be fetched from the database when more rows are
4854
* needed. Use -1 to use the JDBC driver's default configuration.
@@ -61,6 +67,31 @@ public static class Template {
6167
@DurationUnit(ChronoUnit.SECONDS)
6268
private Duration queryTimeout;
6369

70+
/**
71+
* Whether results processing should be skipped. Can be used to optimize callable
72+
* statement processing when we know that no results are being passed back.
73+
*/
74+
private boolean skipResultsProcessing;
75+
76+
/**
77+
* Whether undeclared results should be skipped.
78+
*/
79+
private boolean skipUndeclaredResults;
80+
81+
/**
82+
* Whether execution of a CallableStatement will return the results in a Map that
83+
* uses case-insensitive names for the parameters.
84+
*/
85+
private boolean resultsMapCaseInsensitive;
86+
87+
public boolean isIgnoreWarnings() {
88+
return this.ignoreWarnings;
89+
}
90+
91+
public void setIgnoreWarnings(boolean ignoreWarnings) {
92+
this.ignoreWarnings = ignoreWarnings;
93+
}
94+
6495
public int getFetchSize() {
6596
return this.fetchSize;
6697
}
@@ -85,6 +116,30 @@ public void setQueryTimeout(Duration queryTimeout) {
85116
this.queryTimeout = queryTimeout;
86117
}
87118

119+
public boolean isSkipResultsProcessing() {
120+
return this.skipResultsProcessing;
121+
}
122+
123+
public void setSkipResultsProcessing(boolean skipResultsProcessing) {
124+
this.skipResultsProcessing = skipResultsProcessing;
125+
}
126+
127+
public boolean isSkipUndeclaredResults() {
128+
return this.skipUndeclaredResults;
129+
}
130+
131+
public void setSkipUndeclaredResults(boolean skipUndeclaredResults) {
132+
this.skipUndeclaredResults = skipUndeclaredResults;
133+
}
134+
135+
public boolean isResultsMapCaseInsensitive() {
136+
return this.resultsMapCaseInsensitive;
137+
}
138+
139+
public void setResultsMapCaseInsensitive(boolean resultsMapCaseInsensitive) {
140+
this.resultsMapCaseInsensitive = resultsMapCaseInsensitive;
141+
}
142+
88143
}
89144

90145
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateConfiguration.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* Configuration for {@link JdbcTemplateConfiguration}.
3232
*
3333
* @author Stephane Nicoll
34+
* @author Yanming Zhou
3435
*/
3536
@Configuration(proxyBeanMethods = false)
3637
@ConditionalOnMissingBean(JdbcOperations.class)
@@ -42,11 +43,15 @@ JdbcTemplate jdbcTemplate(DataSource dataSource, JdbcProperties properties,
4243
ObjectProvider<SQLExceptionTranslator> sqlExceptionTranslator) {
4344
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
4445
JdbcProperties.Template template = properties.getTemplate();
46+
jdbcTemplate.setIgnoreWarnings(template.isIgnoreWarnings());
4547
jdbcTemplate.setFetchSize(template.getFetchSize());
4648
jdbcTemplate.setMaxRows(template.getMaxRows());
4749
if (template.getQueryTimeout() != null) {
4850
jdbcTemplate.setQueryTimeout((int) template.getQueryTimeout().getSeconds());
4951
}
52+
jdbcTemplate.setSkipResultsProcessing(template.isSkipResultsProcessing());
53+
jdbcTemplate.setSkipUndeclaredResults(template.isSkipUndeclaredResults());
54+
jdbcTemplate.setResultsMapCaseInsensitive(template.isResultsMapCaseInsensitive());
5055
sqlExceptionTranslator.ifUnique(jdbcTemplate::setExceptionTranslator);
5156
return jdbcTemplate;
5257
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateAutoConfigurationTests.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,35 @@ void testJdbcTemplateExists() {
6666
assertThat(context).hasSingleBean(JdbcOperations.class);
6767
JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);
6868
assertThat(jdbcTemplate.getDataSource()).isEqualTo(context.getBean(DataSource.class));
69+
assertThat(jdbcTemplate.isIgnoreWarnings()).isEqualTo(true);
6970
assertThat(jdbcTemplate.getFetchSize()).isEqualTo(-1);
7071
assertThat(jdbcTemplate.getQueryTimeout()).isEqualTo(-1);
7172
assertThat(jdbcTemplate.getMaxRows()).isEqualTo(-1);
73+
assertThat(jdbcTemplate.isSkipResultsProcessing()).isEqualTo(false);
74+
assertThat(jdbcTemplate.isSkipUndeclaredResults()).isEqualTo(false);
75+
assertThat(jdbcTemplate.isResultsMapCaseInsensitive()).isEqualTo(false);
7276
});
7377
}
7478

7579
@Test
7680
void testJdbcTemplateWithCustomProperties() {
7781
this.contextRunner
78-
.withPropertyValues("spring.jdbc.template.fetch-size:100", "spring.jdbc.template.query-timeout:60",
79-
"spring.jdbc.template.max-rows:1000")
82+
.withPropertyValues("spring.jdbc.template.ignore-warnings:false", "spring.jdbc.template.fetch-size:100",
83+
"spring.jdbc.template.query-timeout:60", "spring.jdbc.template.max-rows:1000",
84+
"spring.jdbc.template.skip-results-processing:true",
85+
"spring.jdbc.template.skip-undeclared-results:true",
86+
"spring.jdbc.template.results-map-case-insensitive:true")
8087
.run((context) -> {
8188
assertThat(context).hasSingleBean(JdbcOperations.class);
8289
JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);
8390
assertThat(jdbcTemplate.getDataSource()).isNotNull();
91+
assertThat(jdbcTemplate.isIgnoreWarnings()).isEqualTo(false);
8492
assertThat(jdbcTemplate.getFetchSize()).isEqualTo(100);
8593
assertThat(jdbcTemplate.getQueryTimeout()).isEqualTo(60);
8694
assertThat(jdbcTemplate.getMaxRows()).isEqualTo(1000);
95+
assertThat(jdbcTemplate.isSkipResultsProcessing()).isEqualTo(true);
96+
assertThat(jdbcTemplate.isSkipUndeclaredResults()).isEqualTo(true);
97+
assertThat(jdbcTemplate.isResultsMapCaseInsensitive()).isEqualTo(true);
8798
});
8899
}
89100

0 commit comments

Comments
 (0)