-
Notifications
You must be signed in to change notification settings - Fork 227
Conservatively handle JDBC MySQL-family batch inserts #3896
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
Open
radovanradic
wants to merge
15
commits into
5.1.x
Choose a base branch
from
mariadb-batch
base: 5.1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
9e27786
Investigate MariaDB batch inserts
radovanradic 2d6e8f0
Investigate MariaDB batch inserts
radovanradic 6a33acf
Trigger build
radovanradic cc2a2e2
More tests
radovanradic d1d2211
Fix MySQL/MariaDB JDBC batch inserts with generated IDs
radovanradic 753359d
Fix MySQL/MariaDB JDBC batch inserts with generated IDs
radovanradic da03c41
Fix MySQL/MariaDB JDBC batch inserts with generated IDs
radovanradic f4e6a47
Mirror conservative batch insert handling for R2DBC
radovanradic 4ff1883
Conservatively fix MySQL-family batch inserts
radovanradic 6016e9e
Remove workflow trigger
radovanradic 218ece3
Update test names and add more verification. Don't change r2dbc behavior
radovanradic 7563027
Cache JDBC batch metadata lazily
radovanradic 6177532
Fix checkstyle
radovanradic ff1ab26
Address review comment.
radovanradic c295c4d
Fix Sonar warning
radovanradic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
data-jdbc/src/test/groovy/io/micronaut/data/jdbc/h2/H2BatchInsertSpec.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| /* | ||
| * Copyright 2017-2026 original authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.micronaut.data.jdbc.h2 | ||
|
|
||
| import io.micronaut.context.ApplicationContext | ||
| import io.micronaut.data.annotation.GeneratedValue | ||
| import io.micronaut.data.annotation.Id | ||
| import io.micronaut.data.annotation.Insert | ||
| import io.micronaut.data.annotation.MappedEntity | ||
| import io.micronaut.data.jdbc.annotation.JdbcRepository | ||
| import io.micronaut.data.model.query.builder.sql.Dialect | ||
| import io.micronaut.data.repository.CrudRepository | ||
| import spock.lang.AutoCleanup | ||
| import spock.lang.Shared | ||
| import spock.lang.Specification | ||
|
|
||
| class H2BatchInsertSpec extends Specification implements H2TestPropertyProvider { | ||
|
|
||
| @AutoCleanup | ||
| @Shared | ||
| ApplicationContext context = ApplicationContext.run(properties) | ||
|
|
||
| @Shared | ||
| H2BatchInsertBookRepository repository = context.getBean(H2BatchInsertBookRepository) | ||
|
|
||
| void setup() { | ||
| repository.deleteAll() | ||
| } | ||
|
|
||
| void "custom void insertAll stores generated-id inserts without mutating input ids"() { | ||
| given: | ||
| def books = [ | ||
| new H2BatchInsertBook(title: "Solaris"), | ||
| new H2BatchInsertBook(title: "Eden") | ||
| ] | ||
|
|
||
| when: | ||
| repository.customInsertAll(books) | ||
| def savedBooks = repository.findAll() | ||
|
|
||
| then: | ||
| books*.id == [null, null] | ||
| savedBooks.size() == 2 | ||
| savedBooks*.id.every { it != null } | ||
| savedBooks*.title as Set == ["Solaris", "Eden"] as Set | ||
| } | ||
|
|
||
| void "custom count insertAll stores generated-id inserts without mutating input ids"() { | ||
| given: | ||
| def books = [ | ||
| new H2BatchInsertBook(title: "Fiasco"), | ||
| new H2BatchInsertBook(title: "The Invincible") | ||
| ] | ||
|
|
||
| when: | ||
| long inserted = repository.customInsertAllCount(books) | ||
| def savedBooks = repository.findAll() | ||
|
|
||
| then: | ||
| inserted == 2 | ||
| books*.id == [null, null] | ||
| savedBooks.size() == 2 | ||
| savedBooks*.id.every { it != null } | ||
| savedBooks*.title as Set == ["Fiasco", "The Invincible"] as Set | ||
| } | ||
| } | ||
|
|
||
| @MappedEntity("h2_batch_insert_book") | ||
| class H2BatchInsertBook { | ||
|
|
||
| @Id | ||
| @GeneratedValue | ||
| Long id | ||
|
|
||
| String title | ||
| } | ||
|
|
||
| @JdbcRepository(dialect = Dialect.H2) | ||
| interface H2BatchInsertBookRepository extends CrudRepository<H2BatchInsertBook, Long> { | ||
|
|
||
| @Insert | ||
| void customInsertAll(List<H2BatchInsertBook> entities) | ||
|
|
||
| @Insert | ||
| long customInsertAllCount(List<H2BatchInsertBook> entities) | ||
| } |
151 changes: 151 additions & 0 deletions
151
data-jdbc/src/test/groovy/io/micronaut/data/jdbc/mariadb/MariaBatchInsertSpec.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| /* | ||
| * Copyright 2017-2026 original authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.micronaut.data.jdbc.mariadb | ||
|
|
||
| import io.micronaut.context.ApplicationContext | ||
| import io.micronaut.data.annotation.GeneratedValue | ||
| import io.micronaut.data.annotation.Id | ||
| import io.micronaut.data.annotation.Insert | ||
| import io.micronaut.data.annotation.MappedEntity | ||
| import io.micronaut.data.jdbc.annotation.JdbcRepository | ||
| import io.micronaut.data.model.query.builder.sql.Dialect | ||
| import io.micronaut.data.repository.CrudRepository | ||
| import spock.lang.AutoCleanup | ||
| import spock.lang.Shared | ||
| import spock.lang.Specification | ||
|
|
||
| class MariaBatchInsertSpec extends Specification implements MariaTestPropertyProvider { | ||
|
|
||
| @AutoCleanup | ||
| @Shared | ||
| ApplicationContext context = ApplicationContext.run(properties) | ||
|
|
||
| @Shared | ||
| MariaBatchBookRepository repository = context.getBean(MariaBatchBookRepository) | ||
|
|
||
| @Shared | ||
| MariaBatchRecordRepository recordRepository = context.getBean(MariaBatchRecordRepository) | ||
|
|
||
| void setup() { | ||
| repository.deleteAll() | ||
| recordRepository.deleteAll() | ||
| } | ||
|
|
||
| void "custom void insertAll stores generated-id inserts without mutating input ids"() { | ||
| given: | ||
| def books = [ | ||
| new MariaBatchBook(title: "The Left Hand"), | ||
| new MariaBatchBook(title: "The Dispossessed") | ||
| ] | ||
|
|
||
| when: | ||
| repository.customInsertAll(books) | ||
| def savedBooks = repository.findAll() | ||
|
|
||
| then: | ||
| savedBooks.size() == 2 | ||
| books*.id == [null, null] | ||
| savedBooks*.id.every { it != null } | ||
| savedBooks*.title as Set == ["The Left Hand", "The Dispossessed"] as Set | ||
| } | ||
|
|
||
| void "custom count insertAll stores generated-id inserts without mutating input ids"() { | ||
| given: | ||
| def books = [ | ||
| new MariaBatchBook(title: "The Lathe of Heaven"), | ||
| new MariaBatchBook(title: "City of Illusions") | ||
| ] | ||
|
|
||
| when: | ||
| long inserted = repository.customInsertAllCount(books) | ||
| def savedBooks = repository.findAll() | ||
|
|
||
| then: | ||
| inserted == 2 | ||
| savedBooks.size() == 2 | ||
| books*.id == [null, null] | ||
| savedBooks*.id.every { it != null } | ||
| savedBooks*.title as Set == ["The Lathe of Heaven", "City of Illusions"] as Set | ||
| } | ||
|
|
||
| void "saveAll generated-key inserts populate ids through fallback path"() { | ||
| given: | ||
| def books = [ | ||
| new MariaBatchBook(title: "A Wizard of Earthsea"), | ||
| new MariaBatchBook(title: "The Tombs of Atuan") | ||
| ] | ||
|
|
||
| when: | ||
| def saved = repository.saveAll(books) | ||
|
|
||
| then: | ||
| saved*.id.every { it != null } | ||
| repository.count() == 2 | ||
| } | ||
|
|
||
| void "saveAll generated-key record inserts populate ids through fallback path"() { | ||
| given: | ||
| def records = (0..<100).collect { new MariaBatchRecord(0L, "name-$it") } | ||
|
|
||
| when: | ||
| List<MariaBatchRecord> saved = recordRepository.saveAll(records) | ||
|
|
||
| then: | ||
| saved.size() == 100 | ||
| saved.collect { it.id() }.every { it != null && it != 0L } | ||
| records.collect { it.id() }.every { it == 0L } | ||
| } | ||
|
|
||
| void "custom void insertAll stores generated-id record inserts without mutating input ids"() { | ||
| given: | ||
| def records = (0..<100).collect { new MariaBatchRecord(0L, "name-$it") } | ||
|
|
||
| when: | ||
| recordRepository.insertAll(records) | ||
| def savedRecords = recordRepository.findAll() | ||
|
|
||
| then: | ||
| records.collect { it.id() }.every { it == 0L } | ||
| savedRecords.size() == 100 | ||
| savedRecords.every { it.id() != null && it.id() != 0L } | ||
| } | ||
| } | ||
|
|
||
| @MappedEntity("maria_batch_book") | ||
| class MariaBatchBook { | ||
|
|
||
| @Id | ||
| @GeneratedValue | ||
| Long id | ||
|
|
||
| String title | ||
| } | ||
|
|
||
| @JdbcRepository(dialect = Dialect.MYSQL) | ||
| interface MariaBatchBookRepository extends CrudRepository<MariaBatchBook, Long> { | ||
|
|
||
| @Insert | ||
| void customInsertAll(List<MariaBatchBook> entities) | ||
|
|
||
| @Insert | ||
| long customInsertAllCount(List<MariaBatchBook> entities) | ||
| } | ||
|
|
||
| @JdbcRepository(dialect = Dialect.MYSQL) | ||
| interface MariaBatchRecordRepository extends CrudRepository<MariaBatchRecord, Long> { | ||
|
|
||
| void insertAll(List<MariaBatchRecord> entities) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.