The Wild

Hibernate JDBC Batch Size: How to Tune It

· 5 min read · Mammals

In this article
  1. What JDBC Batching Actually Does
  2. How to Configure Batch Size in Hibernate
  3. Choosing a Batch Size
  4. Common Problems and Misconceptions
  5. Batch Size vs. Fetch Size and Other Settings
  6. When to Use Batching and Alternatives
  7. Frequently Asked Questions

The Hibernate JDBC batch size is the number of SQL statements Hibernate groups together and sends to the database in a single round trip when batch processing is enabled. It is configured with the property hibernate.jdbc.batch_size (or the JPA equivalent hibernate.jdbc.batch_size in persistence.xml) and is set to a positive integer such as 20, 50, or 100; a value of 0 or a negative number disables batching. This setting only takes effect when hibernate.jdbc.batch_size is greater than zero and the underlying JDBC driver supports batch updates.

What JDBC Batching Actually Does

Without batching, every INSERT, UPDATE, or DELETE statement is sent to the database as a separate network round trip. With batching, Hibernate collects several statements of the same shape and sends them together. The database then executes them as a group, which reduces network latency and can improve throughput significantly for bulk operations.

Hibernate uses the JDBC addBatch() and executeBatch() methods under the hood. The batch size controls how many statements are accumulated before Hibernate calls executeBatch(). For example, with hibernate.jdbc.batch_size=30, Hibernate will send up to 30 INSERT statements in one batch. The actual number can be lower if the session is flushed earlier or if the transaction ends.

Batching works best when the statements are identical in structure, meaning the same SQL with different parameter values. Hibernate can only batch statements that share the same SQL string. If you insert entities with different columns populated (for example, because some fields are null and Hibernate generates different SQL), the batch may be broken into smaller groups or not batched at all.

How to Configure Batch Size in Hibernate

Batch size is set through Hibernate configuration properties. In a typical persistence.xml or hibernate.cfg.xml, you add:

  • hibernate.jdbc.batch_size — the number of statements per batch (e.g., 50).
  • hibernate.order_inserts — set to true to group inserts by entity type, improving batching.
  • hibernate.order_updates — set to true to group updates by entity type.
  • hibernate.batch_versioned_data — set to true if you use versioned entities and want batching for updates.

In Spring Boot, you can set these in application.properties:

spring.jpa.properties.hibernate.jdbc.batch_size=50
spring.jpa.properties.hibernate.order_inserts=true
spring.jpa.properties.hibernate.order_updates=true

If you use a connection pool, ensure the pool does not interfere with batching. Some pools have statement caching that can affect batch behavior, but generally, batching works transparently.

Choosing a Batch Size

There is no single correct batch size. Common values range from 10 to 100, with 20–50 being typical for many applications. The optimal size depends on:

  • Database and driver — some databases have limits on batch size or perform better with smaller batches.
  • Memory — larger batches consume more memory on both the application and database side.
  • Transaction size — a batch is not committed until the transaction commits, so very large batches increase the risk of long-running transactions and lock contention.
  • Statement complexity — simple inserts can handle larger batches than complex updates with many columns.

A good starting point is 30–50. Monitor performance and adjust. If you see diminishing returns or increased memory usage, reduce the size. If batching is not improving performance, check that the driver supports it and that statements are actually being batched (enable Hibernate SQL logging to see batch calls).

Common Problems and Misconceptions

Many developers set hibernate.jdbc.batch_size and expect immediate improvement, but batching can silently fail. Here are frequent issues:

  • Identity generation disables batching — if an entity uses GenerationType.IDENTITY, Hibernate must execute each insert individually to retrieve the generated key, so batching is effectively disabled. Use SEQUENCE or TABLE generation for batch inserts.
  • Versioned entities — updates to versioned entities are not batched by default unless hibernate.batch_versioned_data=true.
  • Different SQL per entity — if you insert entities with different sets of non-null columns, Hibernate may generate different SQL, breaking the batch. Use hibernate.order_inserts=true to group by entity type, but even then, varying column sets can still cause issues.
  • Flush mode — batching only occurs at flush time. If you flush after every save, you defeat batching. Let the session accumulate changes and flush periodically.
  • Driver limitations — some JDBC drivers have bugs or limitations with batch updates. Check your driver documentation.

A common misconception is that batch size controls the number of entities saved in one transaction. It does not; it controls the number of SQL statements per batch. Transaction size is separate and controlled by your application code.

Batch Size vs. Fetch Size and Other Settings

Batch size is often confused with fetch size. Batch size is for write operations (inserts, updates, deletes), while fetch size (hibernate.jdbc.fetch_size) controls how many rows are fetched from the database in one round trip for read operations. They are independent settings.

Also, batch size is not the same as the JDBC batch size used by Spring's JdbcTemplate. Hibernate's batch size applies to its own session flushing. If you use StatelessSession or direct JDBC, you manage batching differently.

When to Use Batching and Alternatives

Batching is beneficial for bulk inserts, updates, or deletes performed through Hibernate. It is less useful for single-row operations or when the database is already fast and network latency is low. For very large data sets, consider:

  • StatelessSession — a Hibernate session without a persistence context, which can be more efficient for bulk operations and supports batching.
  • JDBC batch — using plain JDBC or Spring's JdbcTemplate with batchUpdate for full control.
  • Database-specific bulk loaders — tools like PostgreSQL's COPY or MySQL's LOAD DATA INFILE for extreme volumes.

Batching with Hibernate is convenient but not always the fastest. Measure before and after to ensure it helps your use case.

Frequently Asked Questions

What is the default batch size in Hibernate?

The default is 0, which means batching is disabled. You must explicitly set hibernate.jdbc.batch_size to a positive integer to enable it.

Does batch size affect memory usage?

Yes. Larger batch sizes mean more statements are held in memory before being sent to the database. This can increase heap usage, especially with large entities. Monitor memory and adjust accordingly.

Why is my batch insert not working even though I set batch_size?

Common reasons include using IDENTITY generation, not flushing at the right time, or having different SQL for each insert. Check Hibernate's SQL logs to see if addBatch is being called.

Can I set batch size per session?

Batch size is a global setting for the SessionFactory. You cannot change it per session. However, you can use a separate SessionFactory with different settings if needed.

Is batch size the same as JDBC batch size?

Yes, Hibernate's batch size maps directly to the JDBC batch size used by the underlying driver. It controls how many statements are added to a JDBC batch before execution.