Jersey, Guice and Hibernate - EntityManager thread safety

后端 未结 3 646
轮回少年
轮回少年 2021-01-17 05:40

I have used this tutorial them same way in my application: http://www.benmccann.com/hibernate-with-jpa-annotations-and-guice/

My app is JAX-RS web service which will

3条回答
  •  南笙
    南笙 (楼主)
    2021-01-17 06:16

    If it says that the transaction is already open, that means that it was open by another process and not closed ...

    I suggest to use @Transactionl instead of Writing :

    em.getTransaction().begin();
    

    and

    em.getTransaction().commit();
    em.close();
    

    That will manage the things for you ...

    so for you it will be this way :

    @Transactionl
    @POST
    @Path("/{id}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public void saveItem(){
         entityManager.persist(new Item());
    }
    

    Hope that's help

提交回复
热议问题