Skip to content

Commit 9235345

Browse files
committed
ScriptUtils.executeSqlScript logs SQLWarnings at debug level
Issue: SPR-13959
1 parent 8495fcf commit 9235345

File tree

2 files changed

+31
-21
lines changed

2 files changed

+31
-21
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulator.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -80,8 +80,8 @@ public ResourceDatabasePopulator() {
8080
/**
8181
* Construct a new {@code ResourceDatabasePopulator} with default settings
8282
* for the supplied scripts.
83-
* @param scripts the scripts to execute to initialize or clean up the database;
84-
* never {@code null}
83+
* @param scripts the scripts to execute to initialize or clean up the database
84+
* (never {@code null})
8585
* @since 4.0.3
8686
*/
8787
public ResourceDatabasePopulator(Resource... scripts) {
@@ -97,21 +97,23 @@ public ResourceDatabasePopulator(Resource... scripts) {
9797
* statement can be ignored
9898
* @param sqlScriptEncoding the encoding for the supplied SQL scripts; may
9999
* be {@code null} or <em>empty</em> to indicate platform encoding
100-
* @param scripts the scripts to execute to initialize or clean up the database;
101-
* never {@code null}
100+
* @param scripts the scripts to execute to initialize or clean up the database
101+
* (never {@code null})
102102
* @since 4.0.3
103103
*/
104-
public ResourceDatabasePopulator(boolean continueOnError, boolean ignoreFailedDrops, String sqlScriptEncoding,
105-
Resource... scripts) {
104+
public ResourceDatabasePopulator(boolean continueOnError, boolean ignoreFailedDrops,
105+
String sqlScriptEncoding, Resource... scripts) {
106+
106107
this(scripts);
107108
this.continueOnError = continueOnError;
108109
this.ignoreFailedDrops = ignoreFailedDrops;
109110
setSqlScriptEncoding(sqlScriptEncoding);
110111
}
111112

113+
112114
/**
113115
* Add a script to execute to initialize or clean up the database.
114-
* @param script the path to an SQL script; never {@code null}
116+
* @param script the path to an SQL script (never {@code null})
115117
*/
116118
public void addScript(Resource script) {
117119
Assert.notNull(script, "Script must not be null");
@@ -120,7 +122,7 @@ public void addScript(Resource script) {
120122

121123
/**
122124
* Add multiple scripts to execute to initialize or clean up the database.
123-
* @param scripts the scripts to execute; never {@code null}
125+
* @param scripts the scripts to execute (never {@code null})
124126
*/
125127
public void addScripts(Resource... scripts) {
126128
assertContentsOfScriptArray(scripts);
@@ -130,7 +132,7 @@ public void addScripts(Resource... scripts) {
130132
/**
131133
* Set the scripts to execute to initialize or clean up the database,
132134
* replacing any previously added scripts.
133-
* @param scripts the scripts to execute; never {@code null}
135+
* @param scripts the scripts to execute (never {@code null})
134136
*/
135137
public void setScripts(Resource... scripts) {
136138
assertContentsOfScriptArray(scripts);
@@ -173,8 +175,8 @@ public void setCommentPrefix(String commentPrefix) {
173175
* Set the start delimiter that identifies block comments within the SQL
174176
* scripts.
175177
* <p>Defaults to {@code "/*"}.
176-
* @param blockCommentStartDelimiter the start delimiter for block comments;
177-
* never {@code null} or empty
178+
* @param blockCommentStartDelimiter the start delimiter for block comments
179+
* (never {@code null} or empty)
178180
* @since 4.0.3
179181
* @see #setBlockCommentEndDelimiter
180182
*/
@@ -187,8 +189,8 @@ public void setBlockCommentStartDelimiter(String blockCommentStartDelimiter) {
187189
* Set the end delimiter that identifies block comments within the SQL
188190
* scripts.
189191
* <p>Defaults to <code>"*&#47;"</code>.
190-
* @param blockCommentEndDelimiter the end delimiter for block comments;
191-
* never {@code null} or empty
192+
* @param blockCommentEndDelimiter the end delimiter for block comments
193+
* (never {@code null} or empty)
192194
* @since 4.0.3
193195
* @see #setBlockCommentStartDelimiter
194196
*/
@@ -227,16 +229,16 @@ public void populate(Connection connection) throws ScriptException {
227229
Assert.notNull(connection, "Connection must not be null");
228230
for (Resource script : getScripts()) {
229231
ScriptUtils.executeSqlScript(connection, encodeScript(script), this.continueOnError,
230-
this.ignoreFailedDrops, this.commentPrefix, this.separator, this.blockCommentStartDelimiter,
231-
this.blockCommentEndDelimiter);
232+
this.ignoreFailedDrops, this.commentPrefix, this.separator, this.blockCommentStartDelimiter,
233+
this.blockCommentEndDelimiter);
232234
}
233235
}
234236

235237
/**
236238
* Execute this {@code ResourceDatabasePopulator} against the given
237239
* {@link DataSource}.
238240
* <p>Delegates to {@link DatabasePopulatorUtils#execute}.
239-
* @param dataSource the {@code DataSource} to execute against; never {@code null}
241+
* @param dataSource the {@code DataSource} to execute against (never {@code null})
240242
* @throws ScriptException if an error occurs
241243
* @since 4.1
242244
* @see #populate(Connection)
@@ -254,7 +256,7 @@ final List<Resource> getScripts() {
254256
* {@link EncodedResource} is not a sub-type of {@link Resource}. Thus we
255257
* always need to wrap each script resource in an {@code EncodedResource}
256258
* using the configured {@linkplain #setSqlScriptEncoding encoding}.
257-
* @param script the script to wrap; never {@code null}
259+
* @param script the script to wrap (never {@code null})
258260
*/
259261
private EncodedResource encodeScript(Resource script) {
260262
Assert.notNull(script, "Script must not be null");

spring-jdbc/src/main/java/org/springframework/jdbc/datasource/init/ScriptUtils.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020
import java.io.LineNumberReader;
2121
import java.sql.Connection;
2222
import java.sql.SQLException;
23+
import java.sql.SQLWarning;
2324
import java.sql.Statement;
2425
import java.util.LinkedList;
2526
import java.util.List;
@@ -399,7 +400,7 @@ public static void executeSqlScript(Connection connection, Resource resource) th
399400
*/
400401
public static void executeSqlScript(Connection connection, EncodedResource resource) throws ScriptException {
401402
executeSqlScript(connection, resource, false, false, DEFAULT_COMMENT_PREFIX, DEFAULT_STATEMENT_SEPARATOR,
402-
DEFAULT_BLOCK_COMMENT_START_DELIMITER, DEFAULT_BLOCK_COMMENT_END_DELIMITER);
403+
DEFAULT_BLOCK_COMMENT_START_DELIMITER, DEFAULT_BLOCK_COMMENT_END_DELIMITER);
403404
}
404405

405406
/**
@@ -472,7 +473,14 @@ public static void executeSqlScript(Connection connection, EncodedResource resou
472473
stmt.execute(statement);
473474
int rowsAffected = stmt.getUpdateCount();
474475
if (logger.isDebugEnabled()) {
475-
logger.debug(rowsAffected + " returned as updateCount for SQL: " + statement);
476+
logger.debug(rowsAffected + " returned as update count for SQL: " + statement);
477+
SQLWarning warningToLog = stmt.getWarnings();
478+
while (warningToLog != null) {
479+
logger.debug("SQLWarning ignored: SQL state '" + warningToLog.getSQLState() +
480+
"', error code '" + warningToLog.getErrorCode() +
481+
"', message [" + warningToLog.getMessage() + "]");
482+
warningToLog = warningToLog.getNextWarning();
483+
}
476484
}
477485
}
478486
catch (SQLException ex) {

0 commit comments

Comments
 (0)