Saturday, March 3, 2012

JPA: 2 ways to get container-managed EntityManager in servlet

In Thread-safety when injecting EntityManager post, I wrote about the danger of using field/setter injection to get EntityManager in servlets and compares container-managed EntityManager vs application-managed EntityManager.

What if I need a container-managed EntityManager in my servlet class?
There are 2 ways to do that:

1. type-level injection + JNDI lookup

2. web.xml + JNDI lookup

The persistence-context-ref is declared in web.xml:


No comments :

Post a Comment