geronimo-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Fredrik Jonson <>
Subject Re: EntityManager & Servlets
Date Mon, 31 Aug 2009 11:11:20 GMT
In <> tornike wrote:

>  EntityManager is not thread safe and it should not be injected into the
>  servlet. (container managed one) However in the example of ejb-JPA 
>  I have tested the example works without problems so are there conditions
>  when it can be done or am I understanding it the wrong way?

I'd say the example is wrong or at least careless. So I'm also interested
if someone thinks otherwise.

If I would have done it I'd inject a EntityManagerFactory in the servlet and
acquired a new EntityManager from the pool for each request to doGet method:

public class Example extends HttpServlet {
  private EntityManagerFactory emf;

  protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
      EntityManager em = emf.createEntityManager();
      // do stuff

Fredrik Jonson

View raw message