2 Replies Latest reply on Dec 7, 2009 9:29 PM by Tomas Cerny

    Re-open session or presistence after exception

    Tomas Cerny Novice

      Hello all,


      could someone help me finding a way to re-open session or persistence context or entityManager after an exception happen?


      Let's say I have a program




      public String method() {
      
          em = getEntityManager()
           ...
      
          try {
               // seam call that wont be successful 
         } catch (Exception e)
                // now the context is closed
             
                //I want to save an info to DB
                em.persist(new ErrorRecord(e));  
                // but session is closed   
         }
      
      ...
      }



      I was googling the whole night, tried to get new sessionFactory, entityManagerFacotory,
      I played with ManagedSessionContext..


      Grrrr, why is it that hard?




      Some advice? Thank you in advance


      Tom

        • 1. Re: Re-open session or presistence after exception
          Jason Grosland Newbie

          Did you ever find a resolution for this?  I'm looking at the same problem right now...


          The only thing I can think of is to create another method that starts a new transaction, but not sure if that will work...



          @TransactionAttribute(TransactionAttributeType.REQUIRES NEW)
          public void persistError( ErrorRecord e ) {
              em.persist( e );
          }


          I could also plop the error record on a jms queue, but neither of these solutions really strike me as the way this problem should be solved...


          Separate, unrelated question, why does the wiki formatting fail when I have an underscore in the REQUIRES NEW, and why does that prevent me from posting my response?

          • 2. Re: Re-open session or presistence after exception
            Tomas Cerny Novice

            My friend would say how much will you pay ;) haha..


            But I am better. Even that I spent 16 hours on it ;(


            it is really easy


            Transactions.instance().rollback();
            Transactions.instance().begin();



            Cheers
            Tom