Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Hibernate good for batch processing? What about memory usage?

I have a daily batch process that involves selecting out a large number of records and formatting up a file to send to an external system. I also need to mark these records as sent so they are not transmitted again tomorrow.

In my naive JDBC way, I would prepare and execute a statement and then begin to loop through the recordset. As I only go forwards through the recordset there is no need for my application server to hold the whole result set in memory at one time. Groups of records can be feed across from the database server.

Now, lets say I'm using hibernate. Won't I endup with a bunch of objects representing the whole result set in memory at once?

like image 603
WW. Avatar asked Dec 02 '08 06:12

WW.


People also ask

What is batch processing in Hibernate?

Hibernate Batch processing is an easy way to add multiple statements into a batch and execute that batch by making a single round trip to the database. This tutorial shows how to create batch insert and batch update statements using JPA and Hibernate.

What is batch size in hibernate?

batch_size , the Hibernate documentation recommends a value of between 5 and 30 but this value depends upon the application's needs. The Hibernate documentation's recommendation is suitable for most OLTP-like applications.

What is batch processing in Java?

Typically, batch processing is bulk-oriented, non-interactive, and long running—and might be data- or computation-intensive. Batch jobs can be run on schedule or initiated on demand. Also, since batch jobs are typically long-running jobs, check-pointing and restarting are common features found in batch jobs.

What is Batch_versioned_data?

batch_versioned_data - Some JDBC drivers return incorrect row counts when a batch is executed. If your JDBC driver falls into this category this setting should be set to false .


2 Answers

Hibernate does also iterate over the result set so only one row is kept in memory. This is the default. If it to load greedily, you must tell it so.

Reasons to use Hibernate:

  • "Someone" was "creative" with the column names (PRXFC0315.XXFZZCC12)
  • The DB design is still in flux and/or you want one place where column names are mapped to Java.
  • You're using Hibernate anyway
  • You have complex queries and you're not fluent in SQL

Reasons not to use Hibernate:

  • The rest of your app is pure JDBC
  • You don't need any of the power of Hibernate
  • You have complex queries and you're fluent in SQL
  • You need a specific feature of your DB to make the SQL perform
like image 71
Aaron Digulla Avatar answered Sep 21 '22 09:09

Aaron Digulla


Hibernate offers some possibilities to keep the session small.

You can use Query.scroll(), Criteria.scroll() for JDBC-like scrolling. You can use Session.evict(Object entity) to remove entities from the session. You can use a StatelessSession to suppress dirty-checking. And there are some more performance optimizations, see the Hibernate documentation.

like image 35
cretzel Avatar answered Sep 21 '22 09:09

cretzel