5

So I'm using a simple JpaRepository and the saveAll() method is called.

hibernate.jdbc.batch_size = 500
hibernate.order_inserts = true
hibernate.generate_statistics = true

After running the application:

   8045055 nanoseconds spent acquiring 1 JDBC connections;
    0 nanoseconds spent releasing 0 JDBC connections;
    137189246 nanoseconds spent preparing 1158 JDBC statements;
    1417689514 nanoseconds spent executing 1158 JDBC statements;
    0 nanoseconds spent executing 0 JDBC batches;
    0 nanoseconds spent performing 0 L2C puts;
    0 nanoseconds spent performing 0 L2C hits;
    0 nanoseconds spent performing 0 L2C misses;
    16270990 nanoseconds spent executing 1 flushes (flushing a total of 1158 entities and 0 collections);
    0 nanoseconds spent executing 0 partial-flushes (flushing a total of 0 entities and 0 collections)

Can anyone see a reason for 0 having JDBC batches executed? Also, I have to point that my entity has @GeneratedValue(strategy = IDENTITY) primary key

UnguruBulan
  • 890
  • 4
  • 12
  • 24
  • 3
    [Hibernate disables insert batching at the JDBC level transparently if you use an identity identifier generator.](https://docs.jboss.org/hibernate/orm/current/userguide/html_single/Hibernate_User_Guide.html#batch-session-batch) – Cepr0 Nov 23 '19 at 21:20
  • 1
    you the real MVP. This should be the accepted answer, but you posted a comment – UnguruBulan Nov 25 '19 at 08:59
  • I've posted the answer. ) – Cepr0 Nov 25 '19 at 09:03
  • Maybe [this](https://stackoverflow.com/a/50882952/5380322) my answer about batch insert will be also interesting for you... – Cepr0 Nov 25 '19 at 09:34
  • I'm using a UUID generator on my entities and but I'm using the .save() method for a non-bulk insert and I'm still seeing the same issue? – ennth May 20 '21 at 22:27

3 Answers3

11

Hibernate doesn't perform insert batching with the identity identifier generator. More info is here.

Cepr0
  • 28,144
  • 8
  • 75
  • 101
  • 2
    in my case with oracle insert batching doesn't happen with any of the id generation strategies. and yes i have set the property `hibernate.jdbc.batch_size: 100` – raven Aug 11 '21 at 07:42
  • Does batch insert work when we are setting the Id manually? – Sumit Desai Mar 28 '22 at 07:34
  • The link to the documentation doesn't seem to offer any information about batching with id generator not working. – Jens Schauder Dec 05 '22 at 08:29
1

Set hibernate.order_updates = true also.

Change the logger for org.hibernate.engine.jdbc.batch.internal.BatchingBatch to DEBUG and see what output you are getting.

Chris Savory
  • 2,597
  • 1
  • 17
  • 27
1

Add prefix spring.jpa.properties to property names. It should look like this:

spring.jpa.properties.hibernate.jdbc.batch_size = 500
Conrad
  • 536
  • 5
  • 13