Skip to content

Commit ec6b773

Browse files
committed
Polishing contribution
Closes gh-36032
1 parent fc29d88 commit ec6b773

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

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

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
import org.springframework.util.Assert;
5959
import org.springframework.util.CollectionUtils;
6060
import org.springframework.util.LinkedCaseInsensitiveMap;
61-
import org.springframework.util.StringUtils;
6261

6362
/**
6463
* <b>This is the central delegate in the JDBC core package.</b>
@@ -591,41 +590,34 @@ public int[] batchUpdate(String... sql) throws DataAccessException {
591590
// Callback to execute the batch update.
592591
class BatchUpdateStatementCallback implements StatementCallback<int[]>, SqlProvider {
593592

594-
private @Nullable String currSql;
593+
private final StringBuilder currSql = new StringBuilder();
595594

596595
@Override
597596
public int[] doInStatement(Statement stmt) throws SQLException, DataAccessException {
598597
int[] rowsAffected = new int[sql.length];
599598
if (JdbcUtils.supportsBatchUpdates(stmt.getConnection())) {
600599
for (String sqlStmt : sql) {
601-
this.currSql = appendSql(this.currSql, sqlStmt);
600+
appendSql(sqlStmt);
602601
stmt.addBatch(sqlStmt);
603602
}
604603
try {
605604
rowsAffected = stmt.executeBatch();
606605
}
607606
catch (BatchUpdateException ex) {
607+
this.currSql.setLength(0);
608608
int[] updateCounts = ex.getUpdateCounts();
609-
StringBuilder batchExceptionSql = new StringBuilder();
610-
611-
for (int i = 0; i < Math.min(updateCounts.length, sql.length); i++) {
609+
for (int i = 0; i < ex.getUpdateCounts().length; i++) {
612610
if (updateCounts[i] == Statement.EXECUTE_FAILED) {
613-
if (batchExceptionSql.length() > 0) {
614-
batchExceptionSql.append("; ");
615-
}
616-
batchExceptionSql.append(sql[i]);
611+
appendSql(sql[i]);
617612
}
618613
}
619-
620-
if (batchExceptionSql.length() > 0) {
621-
this.currSql = batchExceptionSql.toString();
622-
}
623614
throw ex;
624615
}
625616
}
626617
else {
627618
for (int i = 0; i < sql.length; i++) {
628-
this.currSql = sql[i];
619+
this.currSql.setLength(0);
620+
this.currSql.append(sql[i]);
629621
if (!stmt.execute(sql[i])) {
630622
rowsAffected[i] = stmt.getUpdateCount();
631623
}
@@ -637,13 +629,16 @@ public int[] doInStatement(Statement stmt) throws SQLException, DataAccessExcept
637629
return rowsAffected;
638630
}
639631

640-
private String appendSql(@Nullable String sql, String statement) {
641-
return (StringUtils.hasLength(sql) ? sql + "; " + statement : statement);
632+
private void appendSql(String statement) {
633+
if (!this.currSql.isEmpty()) {
634+
this.currSql.append("; ");
635+
}
636+
this.currSql.append(statement);
642637
}
643638

644639
@Override
645640
public @Nullable String getSql() {
646-
return this.currSql;
641+
return this.currSql.toString();
647642
}
648643
}
649644

0 commit comments

Comments
 (0)