When does a connection return to the connection pool in a JPA application?

The connection only returns to the connection pool in the JPA application if I call

entityManager.close(); 

?

Can an entitymanger-enabled connection change over its life cycle?

thanks in advance mojoo

+6
source share
2 answers

The JPA specification does not define such things and its implementation for connection management. When a transaction is active, you can be sure that the connection will be the same before committing for obvious reasons. As soon as txn ends, it can be returned or can be saved depending on the implementation (and you do not mention yours)

+4
source

It depends on the implementation and configuration of the JPA.

In EclipseLink, by default, a connection is retained only for the duration of the active (dirty) transaction. those. from the first modification or lock to commit or rollback. For non-transactional requests, a connection is acquired on demand and returned after the request is completed. This allows maximum use of the connection pool. So usually em.close () does nothing.

You can customize this using the storage unit property "eclipselink.jdbc.exclusive-connection.mode". "Always" will contain the connection for the life of the EntityManager.

You can also use different connection pools for transactions, and not for transactional reads. This is useful with JTA, since you can use the DataSource for JTA for reading.

+2
source

All Articles