Skip to content

Commit baa89c5

Browse files
quaffsnicoll
authored andcommitted
Add additional configuration properties for JdbcTemplate.
This commit adds configuration properties for additional settings of the auto-configured JdbcTemplate: * ignore-warnings * skip-results-processing * skip-undeclared-results * results-map-case-insensitive See gh-44470 Signed-off-by: Yanming Zhou <[email protected]>
1 parent 6a5f8ac commit baa89c5

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
*
2828
* @author Kazuki Shimizu
2929
* @author Stephane Nicoll
30+
* @author Yanming Zhou
3031
* @since 2.0.0
3132
*/
3233
@ConfigurationProperties("spring.jdbc")
@@ -61,6 +62,33 @@ public static class Template {
6162
@DurationUnit(ChronoUnit.SECONDS)
6263
private Duration queryTimeout;
6364

65+
/**
66+
* If this variable is {@code false}, we will throw exceptions on SQL warnings.
67+
*/
68+
private boolean ignoreWarnings = true;
69+
70+
/**
71+
* If this variable is set to true, then all results checking will be bypassed for
72+
* any callable statement processing. This can be used to avoid a bug in some
73+
* older Oracle JDBC drivers like 10.1.0.2.
74+
*/
75+
private boolean skipResultsProcessing;
76+
77+
/**
78+
* If this variable is set to true then all results from a stored procedure call
79+
* that don't have a corresponding SqlOutParameter declaration will be bypassed.
80+
* All other results processing will be take place unless the variable
81+
* {@code skipResultsProcessing} is set to {@code true}.
82+
*/
83+
private boolean skipUndeclaredResults;
84+
85+
/**
86+
* If this variable is set to true then execution of a CallableStatement will
87+
* return the results in a Map that uses case-insensitive names for the
88+
* parameters.
89+
*/
90+
private boolean resultsMapCaseInsensitive;
91+
6492
public int getFetchSize() {
6593
return this.fetchSize;
6694
}
@@ -85,6 +113,38 @@ public void setQueryTimeout(Duration queryTimeout) {
85113
this.queryTimeout = queryTimeout;
86114
}
87115

116+
public boolean isIgnoreWarnings() {
117+
return this.ignoreWarnings;
118+
}
119+
120+
public void setIgnoreWarnings(boolean ignoreWarnings) {
121+
this.ignoreWarnings = ignoreWarnings;
122+
}
123+
124+
public boolean isSkipResultsProcessing() {
125+
return this.skipResultsProcessing;
126+
}
127+
128+
public void setSkipResultsProcessing(boolean skipResultsProcessing) {
129+
this.skipResultsProcessing = skipResultsProcessing;
130+
}
131+
132+
public boolean isSkipUndeclaredResults() {
133+
return this.skipUndeclaredResults;
134+
}
135+
136+
public void setSkipUndeclaredResults(boolean skipUndeclaredResults) {
137+
this.skipUndeclaredResults = skipUndeclaredResults;
138+
}
139+
140+
public boolean isResultsMapCaseInsensitive() {
141+
return this.resultsMapCaseInsensitive;
142+
}
143+
144+
public void setResultsMapCaseInsensitive(boolean resultsMapCaseInsensitive) {
145+
this.resultsMapCaseInsensitive = resultsMapCaseInsensitive;
146+
}
147+
88148
}
89149

90150
}

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)
@@ -47,6 +48,10 @@ JdbcTemplate jdbcTemplate(DataSource dataSource, JdbcProperties properties,
4748
if (template.getQueryTimeout() != null) {
4849
jdbcTemplate.setQueryTimeout((int) template.getQueryTimeout().getSeconds());
4950
}
51+
jdbcTemplate.setIgnoreWarnings(template.isIgnoreWarnings());
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
* @author Stephane Nicoll
5353
* @author Kazuki Shimizu
5454
* @author Dan Zheng
55+
* @author Yanming Zhou
5556
*/
5657
class JdbcTemplateAutoConfigurationTests {
5758

@@ -69,21 +70,32 @@ void testJdbcTemplateExists() {
6970
assertThat(jdbcTemplate.getFetchSize()).isEqualTo(-1);
7071
assertThat(jdbcTemplate.getQueryTimeout()).isEqualTo(-1);
7172
assertThat(jdbcTemplate.getMaxRows()).isEqualTo(-1);
73+
assertThat(jdbcTemplate.isIgnoreWarnings()).isEqualTo(true);
74+
assertThat(jdbcTemplate.isSkipResultsProcessing()).isEqualTo(false);
75+
assertThat(jdbcTemplate.isSkipUndeclaredResults()).isEqualTo(false);
76+
assertThat(jdbcTemplate.isResultsMapCaseInsensitive()).isEqualTo(false);
7277
});
7378
}
7479

7580
@Test
7681
void testJdbcTemplateWithCustomProperties() {
7782
this.contextRunner
7883
.withPropertyValues("spring.jdbc.template.fetch-size:100", "spring.jdbc.template.query-timeout:60",
79-
"spring.jdbc.template.max-rows:1000")
84+
"spring.jdbc.template.max-rows:1000", "spring.jdbc.template.ignore-warnings:false",
85+
"spring.jdbc.template.skip-results-processing:true",
86+
"spring.jdbc.template.skip-undeclared-results:true",
87+
"spring.jdbc.template.results-map-case-insensitive:true")
8088
.run((context) -> {
8189
assertThat(context).hasSingleBean(JdbcOperations.class);
8290
JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);
8391
assertThat(jdbcTemplate.getDataSource()).isNotNull();
8492
assertThat(jdbcTemplate.getFetchSize()).isEqualTo(100);
8593
assertThat(jdbcTemplate.getQueryTimeout()).isEqualTo(60);
8694
assertThat(jdbcTemplate.getMaxRows()).isEqualTo(1000);
95+
assertThat(jdbcTemplate.isIgnoreWarnings()).isEqualTo(false);
96+
assertThat(jdbcTemplate.isSkipResultsProcessing()).isEqualTo(true);
97+
assertThat(jdbcTemplate.isSkipUndeclaredResults()).isEqualTo(true);
98+
assertThat(jdbcTemplate.isResultsMapCaseInsensitive()).isEqualTo(true);
8799
});
88100
}
89101

0 commit comments

Comments
 (0)