Browse Source

Use StringBuilder in JdbcTemplate for batch updates

See gh-36032

Signed-off-by: jher235 <tim668666@gmail.com>
main
jher235 1 day ago committed by Brian Clozel
parent
commit
fc29d88778
  1. 18
      spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java

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

@ -605,14 +605,20 @@ public class JdbcTemplate extends JdbcAccessor implements JdbcOperations {
rowsAffected = stmt.executeBatch(); rowsAffected = stmt.executeBatch();
} }
catch (BatchUpdateException ex) { catch (BatchUpdateException ex) {
String batchExceptionSql = null; int[] updateCounts = ex.getUpdateCounts();
for (int i = 0; i < ex.getUpdateCounts().length; i++) { StringBuilder batchExceptionSql = new StringBuilder();
if (ex.getUpdateCounts()[i] == Statement.EXECUTE_FAILED) {
batchExceptionSql = appendSql(batchExceptionSql, sql[i]); for (int i = 0; i < Math.min(updateCounts.length, sql.length); i++) {
if (updateCounts[i] == Statement.EXECUTE_FAILED) {
if (batchExceptionSql.length() > 0) {
batchExceptionSql.append("; ");
}
batchExceptionSql.append(sql[i]);
} }
} }
if (StringUtils.hasLength(batchExceptionSql)) {
this.currSql = batchExceptionSql; if (batchExceptionSql.length() > 0) {
this.currSql = batchExceptionSql.toString();
} }
throw ex; throw ex;
} }

Loading…
Cancel
Save