When to create a new EntityManager

I work with JPA (Eclipselink) in an unmanaged (without EJB) context.

My question is: how long should I keep Entity Manager open?

Swing: - One open Entity Manager while the application is running - Each individual action of the new Entity Manager

Web application: - One entity manager for each (separate) action - one per request - One per session - one per application (-scope), provided that the application is protected by threads

Best wishes

+4
source share
2 answers

For a Swing application, I will choose one EntityManager per action, since it is quite cheap to create it, and you may not want to retain so many privileges that EntityManager manages when you do not use it (this is one EntityManager per application).

For a web application, a typical template is the "OpenSessionInView" template ( Session applied to Hibernate, but it can also be applied to EntityManager , since both logically provide the same role / functionality).

More details here: https://community.jboss.org/wiki/OpenSessionInView

Hope this helps.

+1
source

One object manager for each request should be normal. But they must work through some pool of connections.

0
source

Source: https://habr.com/ru/post/1488111/


All Articles