Skip to content

Conversation

@jher235
Copy link
Contributor

@jher235 jher235 commented Dec 16, 2025

Description

As discussed in issue #36030, this PR refactors the exception handling logic within
JdbcTemplate.batchUpdate(String... sql).

Currently, String concatenation is used inside a loop when constructing the error
message for a BatchUpdateException. This PR replaces it with StringBuilder to
avoid unnecessary temporary object allocation in the failure path.

Changes

  • Replaced String concatenation with StringBuilder: avoids unnecessary temporary
    object allocation for loop-based SQL accumulation.
  • Added Safety Check: uses Math.min(updateCounts.length, sql.length) to safely
    handle edge cases where JDBC drivers may return fewer update counts than the number
    of SQL statements.
  • Preserved Existing Behavior: the happy path logic remains unchanged, and the
    resulting error message format is identical to the previous implementation.

Verification

Verified by the existing CI test suite.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Dec 16, 2025
@bclozel bclozel changed the title Replace String concatenation with StringBuilder in BatchUpdateException Use StringBuilder in JdbcTemplate for batch updates Dec 17, 2025
@bclozel bclozel self-assigned this Dec 17, 2025
@bclozel bclozel added in: data Issues in data modules (jdbc, orm, oxm, tx) type: task A general task and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Dec 17, 2025
@bclozel bclozel added this to the 7.0.3 milestone Dec 17, 2025
bclozel pushed a commit that referenced this pull request Dec 17, 2025
See gh-36032

Signed-off-by: jher235 <tim668666@gmail.com>
@bclozel bclozel closed this in ec6b773 Dec 17, 2025
@bclozel
Copy link
Member

bclozel commented Dec 17, 2025

Thanks for your contribution @jher235 this is now merged in main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in: data Issues in data modules (jdbc, orm, oxm, tx) type: task A general task

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants