11/02/2007

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
("java:comp/env/persistence/em");
} catch (NamingException e) {
throw new ServletException(e);
}
//call EntityManager methods inside transaction
try {
ut.begin();
Employee employee = new Employee(...);
em.persist(employee);
ut.commit();
} 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"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>FooServlet</servlet-name>
<servlet-class>com.foo.abc.FooServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FooServlet</servlet-name>
<url-pattern>/foo</url-pattern>
</servlet-mapping>
<persistence-context-ref>
<persistence-context-ref-name>persistence/em</persistence-context-ref-name>
<persistence-unit-name>my-pu</persistence-unit-name>
</persistence-context-ref>
</web-app>

7 comments:

gobando 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.

Scott 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?

Scott 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.

Scott.

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