Search code examples
javahibernateentitymanagerhibernate-entitymanager

EntityManager per DAO method


I have question about usage of EntityManager. I've read that is not wise, opening and closing an EntityManager for every simple database call in a single thread!

Is it better to have one EntityManager per all DAO methods or one EntityManager per DAO method?


Solution

  • EntityManager should be created, perform a 'Unit of Work', then be closed.

    http://docs.jboss.org/hibernate/stable/entitymanager/reference/en/html_single/#d0e980

    A unit of work would be something like: insert, update, delete, or some more complex business logic. You should get a new EntitiyManager instance for each method, as each method should contain a Unit of Work.

    Update: There is also the concept of Extended EntityManager, which would stay open as long as your application is running, or session is open. This would be managed by the container though.