EntityManager
with @PersistenceContext
into servlet instance variables is not thread-safe, either. EntityManager
is just not designed to be thread-safe.For example, the following code snippet of a servlet class is incorrect:
public class FooServlet extends HttpServlet {One way to fix this is to inject
//This field injection is not thread-safe.
//FIXME
@PersistenceContext
private EntityManager em;
...
}
EntityManagerFactory
instead. EntityManagerFactory
is guaranteed to be thread-safe. For example:public class FooServlet extends HttpServlet {
//This field injection is thread-safe
@PersistenceUnit
private EntityManagerFactory emf;
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
EntityManager em = emf.createEntityManager();
//work with em
}
}