5858import org .springframework .util .Assert ;
5959import org .springframework .util .CollectionUtils ;
6060import 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