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.
James source share