2 Replies Latest reply: Jul 19, 2012 9:04 AM by jtahlborn RSS

    ejb3 isolated (autonomus) transaction/commit

    886995
      I'm using XA (2-phase) transaction. I want to log to one log-table through Log class and Entity Manager. My method inside EJB Session bean looks like:
      private void logError(Throwable throwable) {
          LogEntity logEntity = new LogEntity();
          // Set everything
          entityManager.persist(logEntity);
          entityManager.flush();
      }
      I want to it in isolated (autonomous) transaction independent of any "outer" transaction. I have already tried to add @TransactionAttribute(value = TransactionAttributeType.REQUIRES_NEW) and @TransactionAttribute(value = TransactionAttributeType.NOT_SUPPORTED) before method name and does not work.

      Before I call EJB3 method I create user transaction like:
      try {
          UserTransaction transaction = (UserTransaction)context.lookup("javax.transaction.UserTransaction");
          transaction.begin();
          // Call EJB3 method
          transaction.commit();
      } catch (Throwable t) {
          t.printStackTrace();
          try {
              transaction.rollback();
          } catch (SystemException e) {
              e.printStackTrace();
          }
      }
      I want to Log no matter if commit is done or rollback is done or none. How to? I have already tried to add annotation TransactionAttribute like TransactionAttributeType.NOT_SUPPORTED. Also tried al other values for TransactionAttribute, but without luck.

      Regards
        • 1. Re: ejb3 isolated (autonomus) transaction/commit
          gimbal2
          I want to it in isolated (autonomous) transaction independent of any "outer" transaction. I have already tried to add @TransactionAttribute(value = TransactionAttributeType.REQUIRES_NEW) and @TransactionAttribute(value = TransactionAttributeType.NOT_SUPPORTED) before method name and does not work.
          That is because it is not even an EJB business method; the logError() method is private!

          EJB tech is a subject that fills whole books, you aren't going to be explained how stuff works in a forum post. I did however write an article on the subject that people seem to find very helpful - perhaps it can give you some clues as among other things the article deals with this very subject:

          http://entjavastuff.blogspot.nl/2011/02/ejb-transaction-management-going-deeper.html

          (shameless self-advertisement)
          • 2. Re: ejb3 isolated (autonomus) transaction/commit
            jtahlborn
            gimbal2 wrote:
            I want to it in isolated (autonomous) transaction independent of any "outer" transaction. I have already tried to add @TransactionAttribute(value = TransactionAttributeType.REQUIRES_NEW) and @TransactionAttribute(value = TransactionAttributeType.NOT_SUPPORTED) before method name and does not work.
            That is because it is not even an EJB business method; the logError() method is private!

            EJB tech is a subject that fills whole books, you aren't going to be explained how stuff works in a forum post. I did however write an article on the subject that people seem to find very helpful - perhaps it can give you some clues as among other things the article deals with this very subject:

            http://entjavastuff.blogspot.nl/2011/02/ejb-transaction-management-going-deeper.html
            That is a very good article, and it spells out the problem. It doesn't seem to present the common solution to this problem (at least on my quick read through the article), which is for the ejb to get a local handle to itself and invoke the second method through its own local handle. you can either have a local handle injected to a member variable using standard annotations, or use something like this method to invoke the second method: http://docs.oracle.com/javaee/5/api/javax/ejb/SessionContext.html#getBusinessObject%28java.lang.Class%29