From baa89c5ce877170d2bdfda6beb82c8a655ce1914 Mon Sep 17 00:00:00 2001 From: Yanming Zhou Date: Fri, 28 Feb 2025 09:39:41 +0800 Subject: [PATCH 1/2] 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 --- .../autoconfigure/jdbc/JdbcProperties.java | 60 +++++++++++++++++++ .../jdbc/JdbcTemplateConfiguration.java | 5 ++ .../JdbcTemplateAutoConfigurationTests.java | 14 ++++- 3 files changed, 78 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcProperties.java index 1f316b5dc04..bcd671557c2 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcProperties.java @@ -27,6 +27,7 @@ import org.springframework.boot.convert.DurationUnit; * * @author Kazuki Shimizu * @author Stephane Nicoll + * @author Yanming Zhou * @since 2.0.0 */ @ConfigurationProperties("spring.jdbc") @@ -61,6 +62,33 @@ public class JdbcProperties { @DurationUnit(ChronoUnit.SECONDS) private Duration queryTimeout; + /** + * If this variable is {@code false}, we will throw exceptions on SQL warnings. + */ + private boolean ignoreWarnings = true; + + /** + * If this variable is set to true, then all results checking will be bypassed for + * any callable statement processing. This can be used to avoid a bug in some + * older Oracle JDBC drivers like 10.1.0.2. + */ + private boolean skipResultsProcessing; + + /** + * If this variable is set to true then all results from a stored procedure call + * that don't have a corresponding SqlOutParameter declaration will be bypassed. + * All other results processing will be take place unless the variable + * {@code skipResultsProcessing} is set to {@code true}. + */ + private boolean skipUndeclaredResults; + + /** + * If this variable is set to true then execution of a CallableStatement will + * return the results in a Map that uses case-insensitive names for the + * parameters. + */ + private boolean resultsMapCaseInsensitive; + public int getFetchSize() { return this.fetchSize; } @@ -85,6 +113,38 @@ public class JdbcProperties { this.queryTimeout = queryTimeout; } + public boolean isIgnoreWarnings() { + return this.ignoreWarnings; + } + + public void setIgnoreWarnings(boolean ignoreWarnings) { + this.ignoreWarnings = ignoreWarnings; + } + + 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; + } + } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateConfiguration.java index f3296e6766a..0b664e4755d 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateConfiguration.java @@ -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) @@ -47,6 +48,10 @@ class JdbcTemplateConfiguration { if (template.getQueryTimeout() != null) { jdbcTemplate.setQueryTimeout((int) template.getQueryTimeout().getSeconds()); } + jdbcTemplate.setIgnoreWarnings(template.isIgnoreWarnings()); + jdbcTemplate.setSkipResultsProcessing(template.isSkipResultsProcessing()); + jdbcTemplate.setSkipUndeclaredResults(template.isSkipUndeclaredResults()); + jdbcTemplate.setResultsMapCaseInsensitive(template.isResultsMapCaseInsensitive()); sqlExceptionTranslator.ifUnique(jdbcTemplate::setExceptionTranslator); return jdbcTemplate; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateAutoConfigurationTests.java index 5b27aba76b2..b2465926719 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateAutoConfigurationTests.java @@ -52,6 +52,7 @@ import static org.mockito.Mockito.mock; * @author Stephane Nicoll * @author Kazuki Shimizu * @author Dan Zheng + * @author Yanming Zhou */ class JdbcTemplateAutoConfigurationTests { @@ -69,6 +70,10 @@ class JdbcTemplateAutoConfigurationTests { assertThat(jdbcTemplate.getFetchSize()).isEqualTo(-1); assertThat(jdbcTemplate.getQueryTimeout()).isEqualTo(-1); assertThat(jdbcTemplate.getMaxRows()).isEqualTo(-1); + assertThat(jdbcTemplate.isIgnoreWarnings()).isEqualTo(true); + assertThat(jdbcTemplate.isSkipResultsProcessing()).isEqualTo(false); + assertThat(jdbcTemplate.isSkipUndeclaredResults()).isEqualTo(false); + assertThat(jdbcTemplate.isResultsMapCaseInsensitive()).isEqualTo(false); }); } @@ -76,7 +81,10 @@ class JdbcTemplateAutoConfigurationTests { void testJdbcTemplateWithCustomProperties() { this.contextRunner .withPropertyValues("spring.jdbc.template.fetch-size:100", "spring.jdbc.template.query-timeout:60", - "spring.jdbc.template.max-rows:1000") + "spring.jdbc.template.max-rows:1000", "spring.jdbc.template.ignore-warnings:false", + "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); @@ -84,6 +92,10 @@ class JdbcTemplateAutoConfigurationTests { assertThat(jdbcTemplate.getFetchSize()).isEqualTo(100); assertThat(jdbcTemplate.getQueryTimeout()).isEqualTo(60); assertThat(jdbcTemplate.getMaxRows()).isEqualTo(1000); + assertThat(jdbcTemplate.isIgnoreWarnings()).isEqualTo(false); + assertThat(jdbcTemplate.isSkipResultsProcessing()).isEqualTo(true); + assertThat(jdbcTemplate.isSkipUndeclaredResults()).isEqualTo(true); + assertThat(jdbcTemplate.isResultsMapCaseInsensitive()).isEqualTo(true); }); } From 0e09a2d25f8ff21a6314a83bd7e0df9e5185b2a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Nicoll?= Date: Fri, 7 Mar 2025 10:33:30 +0100 Subject: [PATCH 2/2] Polish "Add additional configuration properties for JdbcTemplate" See gh-44470 --- .../autoconfigure/jdbc/JdbcProperties.java | 43 ++++++++----------- .../jdbc/JdbcTemplateConfiguration.java | 2 +- .../JdbcTemplateAutoConfigurationTests.java | 9 ++-- 3 files changed, 24 insertions(+), 30 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcProperties.java index bcd671557c2..7d879e882fc 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcProperties.java @@ -27,7 +27,6 @@ import org.springframework.boot.convert.DurationUnit; * * @author Kazuki Shimizu * @author Stephane Nicoll - * @author Yanming Zhou * @since 2.0.0 */ @ConfigurationProperties("spring.jdbc") @@ -44,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. @@ -63,32 +68,30 @@ public class JdbcProperties { private Duration queryTimeout; /** - * If this variable is {@code false}, we will throw exceptions on SQL warnings. - */ - private boolean ignoreWarnings = true; - - /** - * If this variable is set to true, then all results checking will be bypassed for - * any callable statement processing. This can be used to avoid a bug in some - * older Oracle JDBC drivers like 10.1.0.2. + * 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; /** - * If this variable is set to true then all results from a stored procedure call - * that don't have a corresponding SqlOutParameter declaration will be bypassed. - * All other results processing will be take place unless the variable - * {@code skipResultsProcessing} is set to {@code true}. + * Whether undeclared results should be skipped. */ private boolean skipUndeclaredResults; /** - * If this variable is set to true then execution of a CallableStatement will - * return the results in a Map that uses case-insensitive names for the - * parameters. + * 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; } @@ -113,14 +116,6 @@ public class JdbcProperties { this.queryTimeout = queryTimeout; } - public boolean isIgnoreWarnings() { - return this.ignoreWarnings; - } - - public void setIgnoreWarnings(boolean ignoreWarnings) { - this.ignoreWarnings = ignoreWarnings; - } - public boolean isSkipResultsProcessing() { return this.skipResultsProcessing; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateConfiguration.java index 0b664e4755d..86a584c19a5 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateConfiguration.java @@ -43,12 +43,12 @@ class JdbcTemplateConfiguration { ObjectProvider 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.setIgnoreWarnings(template.isIgnoreWarnings()); jdbcTemplate.setSkipResultsProcessing(template.isSkipResultsProcessing()); jdbcTemplate.setSkipUndeclaredResults(template.isSkipUndeclaredResults()); jdbcTemplate.setResultsMapCaseInsensitive(template.isResultsMapCaseInsensitive()); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateAutoConfigurationTests.java index b2465926719..30fd043d07f 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/JdbcTemplateAutoConfigurationTests.java @@ -52,7 +52,6 @@ import static org.mockito.Mockito.mock; * @author Stephane Nicoll * @author Kazuki Shimizu * @author Dan Zheng - * @author Yanming Zhou */ class JdbcTemplateAutoConfigurationTests { @@ -67,10 +66,10 @@ 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.isIgnoreWarnings()).isEqualTo(true); assertThat(jdbcTemplate.isSkipResultsProcessing()).isEqualTo(false); assertThat(jdbcTemplate.isSkipUndeclaredResults()).isEqualTo(false); assertThat(jdbcTemplate.isResultsMapCaseInsensitive()).isEqualTo(false); @@ -80,8 +79,8 @@ class JdbcTemplateAutoConfigurationTests { @Test void testJdbcTemplateWithCustomProperties() { this.contextRunner - .withPropertyValues("spring.jdbc.template.fetch-size:100", "spring.jdbc.template.query-timeout:60", - "spring.jdbc.template.max-rows:1000", "spring.jdbc.template.ignore-warnings:false", + .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") @@ -89,10 +88,10 @@ class JdbcTemplateAutoConfigurationTests { 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.isIgnoreWarnings()).isEqualTo(false); assertThat(jdbcTemplate.isSkipResultsProcessing()).isEqualTo(true); assertThat(jdbcTemplate.isSkipUndeclaredResults()).isEqualTo(true); assertThat(jdbcTemplate.isResultsMapCaseInsensitive()).isEqualTo(true);