|
4 | 4 | **Description:** Batch inserts via `EntityManager` in MySQL. This way you can easily control the `flush()` and `clear()` of the persistence context (1st level cache). This is not possible via SpringBoot, `saveAll(Iterable<S> entities)`. Another advantage is that you can call `persist()` instead of `merge()` - this is used behind the scene by the SpringBoot `saveAll(Iterable<S> entities)` and `save(S entity)`. |
5 | 5 |
|
6 | 6 | **Key points:**\ |
7 | | - - in application.properties set `spring.jpa.properties.hibernate.jdbc.batch_size`\ |
8 | | - - in application.properties set `spring.jpa.properties.hibernate.generate_statistics` (just to check that batching is working)\ |
9 | | - - in application.properties set JDBC URL with `rewriteBatchedStatements=true` (optimization for MySQL)\ |
10 | | - - in application.properties set JDBC URL with `cachePrepStmts=true` (enable caching and is useful if you decide to set `prepStmtCacheSize`, `prepStmtCacheSqlLimit`, etc as well; without this setting the cache is disabled)\ |
11 | | - - in application.properties set JDBC URL with `useServerPrepStmts=true` (this way you switch to server-side prepared statements (may lead to signnificant performance boost))\ |
| 7 | + - in `application.properties` set `spring.jpa.properties.hibernate.jdbc.batch_size`\ |
| 8 | + - in `application.properties` set `spring.jpa.properties.hibernate.generate_statistics` (just to check that batching is working)\ |
| 9 | + - in `application.properties` set JDBC URL with `rewriteBatchedStatements=true` (optimization for MySQL)\ |
| 10 | + - in `application.properties` set JDBC URL with `cachePrepStmts=true` (enable caching and is useful if you decide to set `prepStmtCacheSize`, `prepStmtCacheSqlLimit`, etc as well; without this setting the cache is disabled)\ |
| 11 | + - in `application.properties` set JDBC URL with `useServerPrepStmts=true` (this way you switch to server-side prepared statements (may lead to signnificant performance boost))\ |
12 | 12 | - in case of using a parent-child relationship with cascade persist (e.g. one-to-many, many-to-many) then consider to set up `spring.jpa.properties.hibernate.order_inserts=true` to optimize the batching by ordering inserts\ |
13 | 13 | - in entity, use the [assigned generator](https://vladmihalcea.com/how-to-combine-the-hibernate-assigned-generator-with-a-sequence-or-an-identity-column/) since MySQL `IDENTITY` will cause batching to be disabled\ |
14 | | - - in DAO, flush and clear the persistence context from time to time. This way you avoid to "overwhelm" the persistence context. |
| 14 | + - in DAO, flush and clear the persistence context from time to time; this way you avoid to "overwhelm" the persistence context\ |
| 15 | + - if is not needed then ensure that Second Level Cache is disabled via `spring.jpa.properties.hibernate.cache.use_second_level_cache=false` |
15 | 16 |
|
16 | 17 | **Output example:** |
17 | | - |
| 18 | + |
18 | 19 |
|
19 | 20 | -------------------------- |
20 | 21 |
|
|
0 commit comments