It’s been ages since I have posted some sample code. It’s mainly because I don’t have time to collect and post sample code anymore. This once was a bit more challenging and googling wasn’t help much, so now that I have some time I though I would post some sample code that achieves batch inserts with spring data.
For example this link:
http://forum.spring.io/forum/spring-projects/data/118203-bulk-insert-with-crudrepository indicated that I had to manually get the session and iterate/flush (which was true when using Spring/Hibernate/JPA). But when using the CRUDRepository it appears it’s much simpler.
FULL CODE
Full code sample (maven project) can be found on github: https://github.com/cyrus13/anastasakis-net-sample-code/tree/master/spring-data-batch
You basically need to have the following elements:
- Add: ?rewriteBatchedStatements=true to the end of the connectionstring.
- Make sure you use a generator that supports batching in your entity. E.g.
@Id @GeneratedValue(generator = "generator") @GenericGenerator(name = "generator", strategy = "increment")
- Use the: save(Iterable<S> paramIterable); method of the JpaRepository to save the data.
- Use the: hibernate.jdbc.batch_size configuration.
RESULT
So enabling the query log in MySQL:
SET global general_log = 1; SET global log_output = 'table';
we can see the following mysql code is executed:
SET autocommit=0; select max(id) from ExampleEntity; SHOW WARNINGS; select @@session.tx_read_only; insert into ExampleEntity (exampleText, id) values ('de32bec8-1cf9-4f14-b816-0ab7a00b1539', 4), ('c0c85b32-eb2d-4a69-ade4-ac70ea94241c', 5); commit; SET autocommit=1;
Note: Don’t forget to stop logging statements into MySQL general log!
SET global general_log = 0;
Do you know any other generators supports batching ?
Thanks !
Thank you
Thanks for taking out time to write this post. Saved my day bro! Goodluck!
Thanks for this nice workaround! Do you happen to know if it’s safe to use in a multi-threaded environment? Seems like doing this from multiple threads at once can cause havoc.
This didn’t work for me.