Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to DO Batch Update in Hibernate Effectively

I have read many article and found some ways to do batch process

One of that is Using flush and clear , following is the code

        long t1 = System.currentTimeMillis();
        Session session = getSession();
        Transaction transaction = session.beginTransaction();
        try {
            Query query = session.createQuery("FROM PersonEntity WHERE id > " + lastMaxId + " ORDER BY id");
            query.setMaxResults(1000);
            rows = query.list();
            int count = 0;
            if (rows == null || rows.size() == 0) {
                return;
            }
            LOGGER.info("fetched {} rows from db", rows.size());
            for (Object row : rows) {
                PersonEntity personEntity = (PersonEntity) row;
                personEntity.setName(randomAlphaNumeric(30));
                lastMaxId = personEntity.getId();
                session.saveOrUpdate(personEntity);
                if (++count % 50 == 0) {
                    session.flush();
                    session.clear();
                    LOGGER.info("Flushed and Cleared");
                }
            }
        } finally {
            if (session != null && session.isOpen()) {
                LOGGER.info("Closing Session and commiting transaction");
                transaction.commit();
                session.close();
            }
        }
        long t2 = System.currentTimeMillis();
        LOGGER.info("time taken {}s", (t2 - t1) / 1000);

In above code we are processing records in batch of 1000 and updating them in the same transaction .

It is OK when we have to do batch update only .

But I have following questions regading it :

  1. There can be case when some other thread(T2) is accessing the same set of rows for some runtime update operations , but in this case till the 1000 batch will not be commited , T2 remians stuck

So , How we should handle this case ?

Possible thoughts/solution by me :

  1. I think we can do update in different session with small batch of say 50
  2. Use a diffrent Stateless connection for Update and commit the transcation one by one , but close the session when a batch of 1000 completes .

Please Help me getting better solution .

like image 541
Sahil Aggarwal Avatar asked Oct 27 '17 14:10

Sahil Aggarwal


People also ask

What is batch update in hibernate?

1. Overview. In this tutorial, we'll learn how we can batch insert and update entities using Hibernate/JPA. Batching allows us to send a group of SQL statements to the database in a single network call. This way, we can optimize the network and memory usage of our application.

How does Hibernate batch processing work?

Hibernate is storing the freshly inserted objects in the second-level cache. Because of this, there is always the possibility of OutOfMemoryException when Inserting more than one million objects. But there will be situations to inserting huge data into the database.


1 Answers

Do you mean to say this:

  1. there is a batch update in progress inside a transaction

  2. in the meanwhile another thread starts updating one of the records that's there in the batch as well

  3. because of this, the batch will wait till the update in point 2 is complete. This causes the rest of the records in the batch to also wait. So far, it appears all good. However, the important pont here was that the transaction was done to make the update to a large set of records "faster". Usually, transactions are used to ensure "consistency/atomicity". How does one design this piece - fast updates to multiple records in one go with atomicity not being the primary criteria, while a likely update to a record in the batch is also requested by another thread

like image 54
2 revs, 2 users 80% Avatar answered Oct 24 '22 19:10

2 revs, 2 users 80%