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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With