Skip to content

Conversation

@AlexejTimonin
Copy link
Contributor

Feature request: Support for multiple batch updates in NamedParameterJdbcOperations, similar to the existing support in JdbcOperations.
Motivation: In my work, NamedParameterJdbcOperations is often used because of readability. I've come across cases where there is a big collection that needs to be executed in multiple batches. For that I see people doing one of the following:

  • Use classic template, JdbcOperations.batchUpdate(String sql, Collection<T> batchArgs, int batchSize, ParameterizedPreparedStatementSetter<T> pss). It works great, but for many parameters, it can get complex from readability point of view due to many ?.
  • Collection gets manually split into chunks and NamedParameterJdbcOperations.batchUpdate(String sql, SqlParameterSource[] batchArgs) is used. The splitting of the collection can be error prone, and is always done in different ways, for loops, streams. The operation gets cluttered with if / else and loops for the sake of parameter names in the query.

It would be nice to have a multiple batch support in NamedParameterJdbcOperations. So we can replace (example):

        List<SqlParameterSource> newCars = getNewCars();
        String sql = "INSERT INTO CARS VALUES(:year, :brand, :model)";
        int batchSize = 1000;
        List<List<SqlParameterSource>> batches = new ArrayList<>();
        for (int i = 0; i < newCars.size(); i++) {
            if (i % batchSize == 0) {
                batches.add(new ArrayList<>());
            }
            List<SqlParameterSource> currentBatch = batches.getLast();
            currentBatch.add(newCars.get(i));
        }
        for (List<SqlParameterSource> batch : batches) {
            namedParameterJdbcTemplate.batchUpdate(sql, batch.toArray(SqlParameterSource[]::new));
        }

With:

        List<SqlParameterSource> newCars = getNewCars();
        String sql = "INSERT INTO CARS VALUES(:year, :brand, :model)";
        int batchSize = 1000;
        int[][] results = namedParameterJdbcTemplate.batchUpdate(sql, newCars, batchSize);

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Feb 2, 2025
@rstoyanchev rstoyanchev added the in: data Issues in data modules (jdbc, orm, oxm, tx) label Feb 3, 2025
@snicoll snicoll self-assigned this Feb 4, 2025
@snicoll snicoll removed their assignment Feb 14, 2025
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) status: waiting-for-triage An issue we've not yet triaged or decided on

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants