Skip to content

Commit 35facb2

Browse files
Batch inserts (including associations) batch per transaction
1 parent ca0e4ff commit 35facb2

File tree

1 file changed

+5
-2
lines changed
  • HibernateSpringBootBatchInsertOrderBatchPerTransaction

1 file changed

+5
-2
lines changed

HibernateSpringBootBatchInsertOrderBatchPerTransaction/README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
**[How To Optimize Batch Inserts of Parent-Child Relationships In MySQL](https://github.com/AnghelLeonard/Hibernate-SpringBoot/tree/master/HibernateSpringBootBatchInsertOrder)**
1+
**[How To Optimize Batch Inserts of Parent-Child Relationships In MySQL And Batch Per Transaction](https://github.com/AnghelLeonard/Hibernate-SpringBoot/tree/master/HibernateSpringBootBatchInsertOrderBatchPerTransaction)**
22

33
**Description:** Let's suppose that we have a one-to-many relationship between `Author` and `Book` entities. When we save an author, we save his books as well thanks to cascading all/persist. We want to create a bunch of authors with books and save them in the database (e.g., a MySQL database) using the batch technique. By default, this will result in batching each author and the books per author (one batch for the author and one batch for the books, another batch for the author and another batch for the books, and so on). In order to batch authors and books, we need to **order inserts** as in this application.
44

5+
Moreover, this example commits the database transaction after each batch excecution. This way we avoid long-running transactions and, in case of a failure, we rollback only the failed batch and don't lose the previous batches.
6+
57
**Key points:**
6-
Beside all setting specific to batching inserts in MySQL, we need to set up in `application.properties` the following property: `spring.jpa.properties.hibernate.order_inserts=true`
8+
- beside all setting specific to batching inserts in MySQL, we need to set up in `application.properties` the following property: `spring.jpa.properties.hibernate.order_inserts=true`
9+
- in your DAO layer, commit the database transaction after each batch execution
710

811
**Example without ordered inserts:**\
912
![](https://github.com/AnghelLeonard/Hibernate-SpringBoot/blob/master/HibernateSpringBootBatchInsertOrder/batch%20inserts%20including%20associations%20no%20order%20of%20inserts.png)

0 commit comments

Comments
 (0)