4 Replies Latest reply on May 22, 2007 3:35 AM by mskonda

    EJB2.1:UserTransaction with CMT- possible bug?

      I have a CMT bean with a method set to Never as tx attribute.

      - If I get the UserTransction from SessionContext(), I do get an IllegalStateException (java.lang.IllegalStateException: CMT beans are not allowed to get a UserTransaction) which is expected
      - However, I am able to get the same UT from JNDI!

      I am successful in getting TransactionManager and UserTransaction from JNDI in my ejbCreate() method.

      Now, the following method executes without any exceptions/restrictions/constraints:

       private void doNonTxWork()
       {
       logger.info("<< Method: doNonTxWork >>");
       try
       {
       Transaction t = tm.suspend();
       ut.begin();
       logger.info("UserTransaction BEGIN: ");
       ut.commit();
       logger.info("UserTransaction COMMITTED: ");
       tm.resume(t);
       logger.info("Transaction Resumed: ");
       }
       catch (Exception e)
       {
       logger.info("Exception:" + e.getMessage());
       logger.error(e);
       throw new EJBException(e);
       }
       }
      

      Note that the method that's calling this one is set to Never.

      Now, in order to check whether I can do the same work in a Required method(which ideally should'nt work), I called this method from Required method. I didn't get any exceptions/errors and everything seems quite happy!!

      Is it not strange behaviour(possible bug??) or am I missing something here?

      Thanks
      Madhu

        • 1. Re: EJB2.1:UserTransaction with CMT- possible bug?
          waynebaylor

          Why the call to tm.suspend()?

          By marking the method with TransactionAttributeType.NEVER you're stating that there will not be a transaction in progress when the method is called.

          Maybe try using TransactionAttributeType.NOT_SUPPORTED instead of NEVER. That way, if the caller has an associated transaction it will be suspended and restarted after the method executes.

          • 2. Re: EJB2.1:UserTransaction with CMT- possible bug?

            Thanks for your answer Wayne. Irrespective of the attrubute type, the use of UT is prohibited.

            So my point is - the use of UT in CMT is not allowed according to EJB Spec but looks like we have a way to get around this restriction (if I am not missing something obvious :) )

            Whenever my method obtains UT from SessionContext, it throw the expected IllegalStateException which is fine as expected.

            BUT I am able to use JNDI to fetch UT and do the transactional work demarcating programmatically which isn't fine and not expected.

            Regards
            Madhu

            • 3. Re: EJB2.1:UserTransaction with CMT- possible bug?
              waynebaylor

              I just checked out the EJB3 Spec and found the following:

              Section 13.6.5 Handling of Methods that Run with "an unspecified transaction context"


              The term ?an unspecified transaction context? is used in the EJB specification to refer to the cases in
              which the EJB architecture does not fully define the transaction semantics of an enterprise bean method
              execution.

              This includes the following cases:

              ? The execution of a method of an enterprise bean with container-managed transaction demarcation
              for which the value of the transaction attribute is NOT_SUPPORTED, NEVER, or SUPPORTS.

              ? The execution of a PostConstruct, PreDestroy, PostActivate, or PrePassivate
              callback method of a session bean with container-managed transaction demarcation.[71]

              ? The execution of a PostConstruct or PreDestroy callback method of a message-driven
              bean with container-managed transaction demarcation.[72]

              The EJB specification does not prescribe how the container should manage the execution of a method
              with an unspecified transaction context?the transaction semantics are left to the container implementation.


              My guess is that JBoss lets you use UserTransaction since there's no tx context defined.

              • 4. Re: EJB2.1:UserTransaction with CMT- possible bug?

                 

                The execution of a method of an enterprise bean with container-managed transaction demarcation
                for which the value of the transaction attribute is NOT_SUPPORTED, NEVER, or SUPPORTS.


                However Wayne, I am able to use UT from a REQUIRED method too, which I think the above quote doesn't support.

                Thanks
                Madhu