Home > Blockchain >  Hibernate JPA DB closing connection when persisting many entities
Hibernate JPA DB closing connection when persisting many entities

Time:09-17

I have a list of about 20 000 entities that are looped though and added to the EntityManager using the persists method like this:¨

public final void createE(e entity) {
    em.persist(entity);
}

So what could be wrong here? Why is the connection getting closed? Isn't Hibernate supposed to handle that and set up a new connection when needed?

CodePudding user response:

Have you configured the batch size?

For example:

hibernate.jdbc.batch_size 200

https://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/batch.html

CodePudding user response:

Isn't Hibernate supposed to handle that and set up a new connection when needed?

Nope, that is not how transactions work. What you, as the developer can do is create and commit a transaction for every e.g. 1000 entities or whatever batch/chunk size works for you. Note that the connection closing probably happens because the network is not reliable between your computer and the server, whereas a localhost connection obviously is perfectly reliable. It takes longer because of bandwidth limitations or higher latency or maybe even simply because the database server is busy doing other IO.

  • Related