Injecting an EntityManager into a servlet is not recommended since the former 
is not thread safe. Here is what I do in my current web application:


  | @PersistenceUnit(name = "myPU")
  | private EntityManagerFactory factory;
  | 
  | protected void doGet(HttpServletRequest request,
  |     HttpServletResponse response) throws ServletException,
  |             IOException{
  |     try{
  |             if(factory == null){
  |                     String name = "java:/myEntityManagerFactory";
  |                     factory = (EntityManagerFactory)
  |                             Naming.getInitialContext().lookup(name);
  |                     }
  |                     if(em == null){
  |                             em = factory.createEntityManager();
  |                     }
  |             }
  |     }catch(NamingException e){
  |             err.println("NamingException when trying to get an " +
  |                     "EntityManager: "+e.getMessage());
  |             e.printStackTrace();
  |     }
  | }
  | 
  | 
  | 

View the original post : 
http://www.jboss.com/index.html?module=bb&op=viewtopic&p=3965218#3965218

Reply to the post : 
http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=3965218
_______________________________________________
jboss-user mailing list
[email protected]
https://lists.jboss.org/mailman/listinfo/jboss-user

Reply via email to