According to this book :
Creating an EntityManager starts its persistence context. Hibernate won’t access
the database until necessary; the EntityManager doesn’t obtain a JDBC Connection
from the pool until SQL statements have to be executed. You can create and close an EntityManager without hitting the database
My super simple program is this:
EntityManager outerEm = emf.createEntityManager(); logger.info("Connection is closed [after create em] "+outerEm.unwrap(Session.class).doReturningWork(Connection::isClosed));
and surprisingly the log says : - Connection is closed [after create em] false
Why entity manager holds db connection if it has no SQL statement to execute?