geronimo-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Fredrik Jonson <fred...@myrealbox.com>
Subject Re: EntityManager & Servlets
Date Mon, 31 Aug 2009 11:11:20 GMT
In <25221488.post@talk.nabble.com> 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 
>  http://cwiki.apache.org/GMOxDOC21/container-managed-persistence-with-jpa.html
>
>  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 {
  @PersistenceUnit
  private EntityManagerFactory emf;

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

-- 
Fredrik Jonson


Mime
View raw message