How to get container-managed EntityManager in servlets

In this previous post, I wrote about the danger of using field/setter injection to get EntityManager in servlets A follow-up post 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

@PersistenceContext(unitName="my-pu", name="persistence/em")
public class FooServlet extends HttpServlet {
@Resource private UserTransaction ut;

protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
EntityManager em = null;
try {
Context ic = new InitialContext();
em = (EntityManager) ic.lookup
} catch (NamingException e) {
throw new ServletException(e);
//call EntityManager methods inside transaction
try {
Employee employee = new Employee(...);
} catch (Exception e) {
throw new ServletException(e);

2. web.xml + JNDI lookup

public class FooServlet extends HttpServlet {
@Resource private UserTransaction ut;
//the rest is the same as listed in bullet 1

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

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">


Anonymous said...

There's something that i still don't understand. When you inject the PersistenceContext, you give your PU name and another name. Checkin' my persistence.xml i realized that there's only this tag: "persistence-unit name="PmEJB3.0"(it'd be "my-pu" in this example)
but, can i add another "name" property??

javahowto said...

The name attribute of @PersistenceContext is used to look up the injected entity manager. It has no corresponding element in persistence.xml. This name can be specified either as an annotation attribute, or in web.xml, as illustrated in the post.

Entity managers associated with the same persistence unit can be injected into multiple places and each can have a different name. That's why you can't specify this name in persistence.xml.

Unknown said...

Hi guys, thanks for the tutorial.

I tried following but i get the following exception when instantiating the Entity Manager: javax.servlet.ServletException: Name persistence is not bound in this Context

Any ideas whats wrong?

Cheers, Scott.

javahowto said...

Looks like the name entry (persistence/em) is not bound in the web app's naming context. Are you sure you already have either the type-level @PersistenceContext injection (the first line in the above sample code), or declaration in web.xml?

Unknown said...

Hiya, thanks for the quick reply.

I have tried both methods (@persistence context and declaring it in my web.xml) and neither is working. I even tried a combination of both and then when that didn't work I tried doing away with my servlet altogether and just using your fooservlet and that didn't work either. I am deploying to tomcat 5.5 and am using the spring framework. Perhaps i am missing a jar somewhere that is needed???

Thanks for your help.


javahowto said...

Tomcat as a web container may not offer platform services like @PersistenceContext injection, container managed entity managers, which are available in a full appserver. 5.5 is a pretty old version (Maybe dated before JPA). You can try application-managed entity manager by using EntityManagerFactory.createEntityManager(...).

Anna said...

Great and Useful Article.

Online Java Course

Java Online Training

Java Course Online

J2EE training

online J2EE training

Best Recommended books for Spring framework

Java Interview Questions

Java Training Institutes in Chennai

Java Training in Chennai

J2EE Training in Chennai

java j2ee training institutes in chennai

Unknown said...

I enjoyed on reading your blog post. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. Please visit my website, Friv 4000 Games is where all the free friv games.
Friv 4000

eMexoT said...

Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.

Internet Of Things training in electronic city