Sahil Aggarwal
Sahil Aggarwal

Reputation: 1351

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 .

Upvotes: 9

Views: 1433

Answers (1)

Prince Singh
Prince Singh

Reputation: 109

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

Upvotes: 1

Related Questions