0

Well, i'm trying to making a batch insert in JPA but i think this don't work.

My method is this :

public void saveBatch(List<? extends AbstractBean> beans) {
    try {
        begin();
        logger.info("Salvando em batch " + beans.size() + " bean(s)");
        for (int i = 0; i < beans.size(); i++) {
        if (i % 50 == 0) {
            entityManager.flush();
            entityManager.clear();
        }
        entityManager.merge(beans.get(i));

        }
        commit();
    } catch (Exception e) {
        logger.error("Ocorreu um erro ao tentar salvar batch. MSG ORIGINAL: "
                + e.getMessage());
        rollback();
        throw new DAOException("Ocorreu um erro ao tentar salvar batch");
    }
    }

My ideia is that each 50 rows the hibernate will make:

insert into tableA values (),(),()...

But watching the log i see one INSERT for each merge() command link this:

insert into tableA values ()
insert into tableA values ()
insert into tableA values ()
insert into tableA values ()

What is wrong ? This is correct ?

8
  • what is your JPA impl. It is depending on. Have you modify your JPA conf to enable it ? If yes, how ? Commented Jul 17, 2016 at 17:47
  • my jpa is 2.1, using hibernate. I not modified nothing in jpa config. Commented Jul 17, 2016 at 17:57
  • Possible duplicate stackoverflow.com/questions/20285347/… Commented Jul 17, 2016 at 19:12
  • Your assumptions are wrong. Hibernate will not generate a single bulky SQL statement but it will rather take a batch of (insert) statements (50 in your case) and pass them onto the database simultaneously in a single round trip (when EntityManager#flush() (or commit at the end) is executed) Commented Jul 17, 2016 at 20:00
  • I read this post but i am already using entityManager and a lowest value batch size = 50 Commented Jul 17, 2016 at 20:01

1 Answer 1

1

Hibernate does not enable batching by default. You will want to consider the following settings (I think at least batch_size is required to get any batch inserts/updates to work):

hibernate.jdbc.batch_size

A non-zero value enables use of JDBC2 batch updates by Hibernate. e.g. recommended values between 5 and 30

hibernate.jdbc.batch_versioned_data

Set this property to true if your JDBC driver returns correct row counts from executeBatch(). It is usually safe to turn this option on. Hibernate will then use batched DML for automatically versioned data. Defaults to false. e.g. true | false

hibernate.order_updates (similarly, hibernate.order_inserts)

Forces Hibernate to order SQL updates by the primary key value of the items being updated. This will result in fewer transaction deadlocks in highly concurrent systems. e.g. true | false

Sign up to request clarification or add additional context in comments.

1 Comment

I added batch_size in persistente.xml but the problem continue. I cant understand why i should use this property if i am controlling it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.