Skip to content

Commit fc29d88

Browse files
jher235bclozel
authored andcommitted
Use StringBuilder in JdbcTemplate for batch updates
See gh-36032 Signed-off-by: jher235 <tim668666@gmail.com>
1 parent 033df68 commit fc29d88

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -605,14 +605,20 @@ public int[] doInStatement(Statement stmt) throws SQLException, DataAccessExcept
605605
rowsAffected = stmt.executeBatch();
606606
}
607607
catch (BatchUpdateException ex) {
608-
String batchExceptionSql = null;
609-
for (int i = 0; i < ex.getUpdateCounts().length; i++) {
610-
if (ex.getUpdateCounts()[i] == Statement.EXECUTE_FAILED) {
611-
batchExceptionSql = appendSql(batchExceptionSql, sql[i]);
608+
int[] updateCounts = ex.getUpdateCounts();
609+
StringBuilder batchExceptionSql = new StringBuilder();
610+
611+
for (int i = 0; i < Math.min(updateCounts.length, sql.length); i++) {
612+
if (updateCounts[i] == Statement.EXECUTE_FAILED) {
613+
if (batchExceptionSql.length() > 0) {
614+
batchExceptionSql.append("; ");
615+
}
616+
batchExceptionSql.append(sql[i]);
612617
}
613618
}
614-
if (StringUtils.hasLength(batchExceptionSql)) {
615-
this.currSql = batchExceptionSql;
619+
620+
if (batchExceptionSql.length() > 0) {
621+
this.currSql = batchExceptionSql.toString();
616622
}
617623
throw ex;
618624
}

0 commit comments

Comments
 (0)