Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between Container Managed and Application Managed EntityManager

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.

like image 858
Wael Avatar asked Nov 21 '12 08:11

Wael


Video Answer


1 Answers

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.

like image 132
Arjan Tijms Avatar answered Nov 15 '22 09:11

Arjan Tijms