# Hibernate issuing individual insert statements, even though batch insert is enabled

**URL:** <https://discourse.hibernate.org/t/hibernate-issuing-individual-insert-statements-even-though-batch-insert-is-enabled/2014>\
**Category:** Hibernate ORM\
**Created:** [January 10, 2019, 10:04pm UTC](https://discourse.hibernate.org/t/hibernate-issuing-individual-insert-statements-even-though-batch-insert-is-enabled/2014 "2019-01-10T22:04:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rck](https://avatars.discourse-cdn.com/v4/letter/r/c37758/32.png) [@Rck](https://discourse.hibernate.org/u/Rck)\
**Post date:** [January 10, 2019, 10:04pm UTC](https://discourse.hibernate.org/t/hibernate-issuing-individual-insert-statements-even-though-batch-insert-is-enabled/2014/1 "2019-01-10T22:04:37Z")

</div>

In our Spring-data-JPA (2.1.0) using Hibernate 5.3.7, we are using following settings to enable the batch processing.

```auto
spring.jpa.properties.hibernate.jdbc.batch_size=15
spring.jpa.properties.hibernate.order_inserts=true
spring.jpa.properties.hibernate.order_updates=true
spring.jpa.properties.hibernate.jdbc.batch_versioned_data=true
spring.jpa.properties.hibernate.id.new_generator_mappings=false

```

and in our service,

```auto
List<T> entities = new ArrayList<>();
if(entities.size() % 500 == 0){

        repository.saveAll(entities);
        repository.flush();
        entities.clear()
}
entities.add(e);

```

So, the code is basically calling saveAll for every 500 records, and the batch\_size is defined as 15. Does this difference have any impact on the way Hibernate behaves with respect to insertions?

But what I noticed is, I see individual insert statements 500 times, when flush is called. Is this expected behavior when hibernate.jdbc.batch\_size is set?

How can I ensure that Hibernate is actually performing a batch insert if at all doing?

Also, does Hibernate provide any feature to issue an insert multi rows if Oracle database is used? I read, PostgreSQL supported with a setting on JDBC URL, but can this be achieved if Oracle is used instead, without using NativeQuery

Thanks

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [January 11, 2019, 5:38am UTC](https://discourse.hibernate.org/t/hibernate-issuing-individual-insert-statements-even-though-batch-insert-is-enabled/2014/2 "2019-01-11T05:38:23Z")

</div>

> But what I noticed is, I see individual insert statements 500 times, when flush is called. Is this expected behavior when hibernate.jdbc.batch\_size is set?

If you are logging SQL statements with Hibernate, you cannot tell if batching is used.

You need to use a tool like `datasource-proxy` or `p6spy` to see that JDBC batching is actually used. Check out [this article](https://vladmihalcea.com/the-best-way-to-log-jdbc-statements/) for more details.

> Also, does Hibernate provide any feature to issue an insert multi rows if Oracle database is used? I read, PostgreSQL supported with a setting on JDBC URL, but can this be achieved if Oracle is used instead, without using NativeQuery

PostgreSQL supports multi-row inserts via the [`reWriteBatchedInserts`](https://vladmihalcea.com/postgresql-multi-row-insert-rewritebatchedinserts-property/) configuration property. However, that’s needed because, although you’re using a single database roundtrip, PostgreSQL processes each statement individually.

Oracle might not require multi-row inserts, so just make sure you are using `SEQUENCE` identifiers as `IDENTITY` generators disable batched inserts as explained in [this article](https://vladmihalcea.com/how-to-batch-insert-and-update-statements-with-hibernate/).

More, the batch processing example can be optimized too. Instead of just clear-and-flush, you want to commit the DB transaction from time to time to avoid a long-running transaction:

```auto
for (int i = 0; i < entityCount; i++) {
	if (i > 0 && i % batchSize == 0) {
		entityTransaction.commit();
		entityTransaction.begin();

		entityManager.clear();
	}

	Post post = new Post(
		String.format("Post %d", i + 1)
	);
	 
	entityManager.persist(post);
}

```

Check out [this article](https://vladmihalcea.com/the-best-way-to-do-batch-processing-with-jpa-and-hibernate/) if you want to know the best way to do batch processing with JPA and Hibernate.

---

<div class="post-metadata">

**Author:** ![Rck](https://avatars.discourse-cdn.com/v4/letter/r/c37758/32.png) [@Rck](https://discourse.hibernate.org/u/Rck)\
**Post date:** [January 11, 2019, 4:58pm UTC](https://discourse.hibernate.org/t/hibernate-issuing-individual-insert-statements-even-though-batch-insert-is-enabled/2014/3 "2019-01-11T16:58:08Z")

</div>

Thanks Vlad, I will refer to the links provided. Currently ID is defined as below

```
@Id
@Basic
@GeneratedValue(generator = "ENT_SEQ", strategy = GenerationType.SEQUENCE)
@SequenceGenerator(name = "ENT_SEQ", sequenceName = "ENT_SID_SEQ")
@Column(name = "ENT_SID")
protected int entSID;

CREATE SEQUENCE "ENT_SID_SEQ" MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 1 NOCACHE NOORDER NOCYCLE NOPARTITION ;

```

I hope my sequence def is correct.

> “More, the batch processing example can be optimized too. Instead of just clear-and-flush, you want to commit the DB transaction from time to time to avoid a long-running transaction:”

Unfortunately, we can’t do that, as the requirement is to do all or nothing and each call requires us to persist several thousand of records. We need to persist all or nothing.

From the initial round of testing, we noticed around 15 - 20 secs to persist about 10K. Which seems to be high?

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [January 11, 2019, 5:00pm UTC](https://discourse.hibernate.org/t/hibernate-issuing-individual-insert-statements-even-though-batch-insert-is-enabled/2014/4 "2019-01-11T17:00:43Z")

</div>

> [@Rck](#):
>
> From the initial round of testing, we noticed around 15 - 20 secs to persist about 10K. Which seems to be high?

You need to profile the batch process and to break down the response time into call stack. Then, you will know for sure why it takes so much time and which component causes the slow processing.

---

<div class="post-metadata">

**Author:** ![ali.mujuthaba](https://avatars.discourse-cdn.com/v4/letter/a/a9a28c/32.png) [@ali.mujuthaba](https://discourse.hibernate.org/u/ali.mujuthaba)\
**Post date:** [June 3, 2020, 3:52pm UTC](https://discourse.hibernate.org/t/hibernate-issuing-individual-insert-statements-even-though-batch-insert-is-enabled/2014/5 "2020-06-03T15:52:51Z")

</div>

> [@vlad](#):
>
> More, the batch processing example can be optimized too. Instead of just clear-and-flush, you want to commit the DB transaction from time to time to avoid a long-running transaction

I am sorry I am just trying to understand the rationale behind what you said. I understood what you said about long running transaction but many times we would want the batch to fail as a whole if it ever does right, so we can re-run it from the beginning?
