In <[email protected]> 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