3 Replies Latest reply on Oct 14, 2009 1:07 PM by kapitanpetko

    EntityManager transaction in a Seam managed Servlet

    giomiano

      Hi guys,
      I create a servelet managed by seam (web:context-filter), something like:



      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      
      EntityManager entityManager = (EntityManager)Component.getInstance("entityManager");|
      
      entityManager.persist(a);
      
      entityManager.flush;
      }




      I can easily fetch object using entityManager.find, createQuery etc.. but when I try to persist I get the following exception.


      TransactionRequiredException: no transaction is in progress


      I tried also entityManager.getTransaction().begin() and I get the same result.


      I can even immagine why is that but how do I find a work-around ?


      Thanks

        • 1. Re: EntityManager transaction in a Seam managed Servlet
          giomiano

          Actually I did not reliazed about Transaction ;)


          Transaction.instance().begin();



          Transaction.instance().commit();



          Hope helps in future

          • 2. Re: EntityManager transaction in a Seam managed Servlet
            jonathanb

            I am attempting something similar but can't seem to get a result. My query in the servlet works fine, but my update appears not to generate sql (based on hibernate show sql true) and I get no effect in the database. I have:


            EntityManager em = (EntityManager) Component.getInstance("entityManager");
            



            ...



            UserTransaction txn = Transaction.instance();
            txn.begin();
            enquiry.setStatus(status);
            em.persist(enquiry);
            txn.commit();
            



            My servlet includes an @Name annotation, other than that its a standard servlet. There is no conversationId being passed to the servlet. Any help would be much appreciated.

            • 3. Re: EntityManager transaction in a Seam managed Servlet
              kapitanpetko

              Since there is no tx active when you call getInstance, your entity manager
              knows nothing about your transaction. You need to call em.joinTransaction() after txn.begin().