Differences between Container Managed and Application Managed EntityManager

坚强是说给别人听的谎言 提交于 2019-12-23 10:55:39

问题


I have a problem to understand the differences between container-managed and application-managed entity manager?

I would be very grateful if you can give me an example that illustrates the differences.


回答1:


For a container-managed entity manager, the container manages the life-cycle of this entity manager. For an application-managed one, the application (meaning you, the programmer) manages this.

A simple but very visible difference is that you must call close() on an application-managed entity factory. When you use a container-managed one, the container does this for you.

The persistence context of an application-managed entity manager is also not transaction scoped. It starts when the entity manager is created and ends when it is closed. This makes it somewhat like the extended persistence context, with the difference that you can use this type of entity manager everywhere, not just in stateful beans.

Finally, application-managed entity managers are the only ones that can be officially configured to use resource-local transactions, which are independent of any (JTA) transaction the container might have running.

Note that in Java SE you only have application-managed entity managers. So, when you just add Hibernate to Tomcat (a popular combination), you're essentially working with application-managed entity managers all the time.




回答2:


If you have multiple java EE application connecting same database and using JPA entity manager cache. you may face this issue:

Database changes made in one application may not reflected in other applications immediately

Use application managed entity manager instead of container managed entity manager is the perfect solution to resolve this issue.

You may refer to my blog Java EE Example - How to use application managed entitymanager



来源:https://stackoverflow.com/questions/13489057/differences-between-container-managed-and-application-managed-entitymanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!