Skip to content

GH-1159 Batch Insert Operations Within a Single Aggregate #1191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

ctailor2
Copy link
Contributor

@ctailor2 ctailor2 commented Mar 3, 2022

This improves the strategy for inserting entities referenced by aggregate to perform a batch insert when there are multiple entities in a collection.

This is only possible for databases which support retrieving generated keys for batch operations through Statement#getGeneratedKeys, which includes all currently supported databases other than DB2 and MS SQL Server. Note: DB2 does support this functionality outside of the JDBC spec with DB2PreparedStatement#getDBGeneratedKeys.

This implementation includes low level code in BatchJdbcOperations for performing the batch insert with a KeyHolder and String[] keyColumnNames which has largely been copied from spring framework spring-jdbc, pending the acceptance of a proposal to add native support via additional NamedParameterJdbcOperations#batchUpdate methods. If and when this proposal is accepted and merged, BatchJdbcOperations can be deleted and callers into it can be replaced with calls into NamedParameterJdbcOperations.

Caveats:

  • As part of the construction of the AggregateChange to be executed, inserts for a given referenced collection of entities are grouped into batches based on the presence/absence of an id value. Since this is determined only once rather than with every jdbc operation, this will produce unexpected results for any existing BeforeSaveCallback that is setting the value of an @Id annotated property. Any such operation will need to be moved to BeforeConvertCallback in order to continue working as expected.
  • Batch insert has not been implemented for MyBatisDataAccessStrategy at this time. When used, this will process the batch insert as serial inserts.

Closes #1159

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 3, 2022
@ctailor2 ctailor2 force-pushed the issue/1159-batch-insert-within-aggregate branch from 213a801 to e5d6272 Compare March 3, 2022 21:19
@ctailor2 ctailor2 changed the title Issue/1159 batch insert within aggregate GH-1159 Batch Insert Operations Within a Single Aggregate Mar 3, 2022
@ctailor2 ctailor2 marked this pull request as ready for review March 3, 2022 22:08
@ctailor2 ctailor2 requested a review from schauder March 3, 2022 22:08
@ctailor2 ctailor2 requested a review from schauder March 8, 2022 17:56
Copy link
Contributor

@schauder schauder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As for the old method signatures: They should behave as close to the original as possible. If they don't work for certain cases we need to identify those cases in order to decide if we can go forward with this, or if we have to move the change to the next major release.

@schauder
Copy link
Contributor

And please squash the actual feature in a single commit ,please.

@ctailor2
Copy link
Contributor Author

As for the old method signatures: They should behave as close to the original as possible. If they don't work for certain cases we need to identify those cases in order to decide if we can go forward with this, or if we have to move the change to the next major release.

I removed my previous default implementation for the deprecated DataAccessStrategy#insert method and added a more thorough concrete implementation that works for all values of IdValueSource.

@ctailor2 ctailor2 force-pushed the issue/1159-batch-insert-within-aggregate branch from 370e08f to 295ac39 Compare March 15, 2022 17:45
@ctailor2 ctailor2 requested a review from schauder March 15, 2022 17:46
+ Group into batches when populating AggregateChange based on the
presence/absence of a value for @id.
+ Add Dialect IdGeneration property to indicate support for
batch operations. No support by SqlServer and DB2 Dialects.
@ctailor2 ctailor2 force-pushed the issue/1159-batch-insert-within-aggregate branch from 295ac39 to 26904fc Compare March 15, 2022 19:06
@schauder
Copy link
Contributor

Thanks, that's merged.

@schauder schauder closed this Mar 16, 2022
@schauder schauder deleted the issue/1159-batch-insert-within-aggregate branch March 17, 2022 08:36
schauder added a commit that referenced this pull request Sep 18, 2023
BatchJdbcOperations is still there, but deprecated, and not used except for deprecated places kept for backward compatibility.

This is possible since Spring Framework made the features offered by `BatchJdbcOperations` part of `NamedParameterJdbcOperations`.

See spring-projects/spring-framework#28132
See #1191
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Batch insert operations within a single aggregate.
3 participants