4 Replies Latest reply on Apr 10, 2006 11:26 AM by hitcher

    contradiction between the Sun tutorial and JBoss-Wiki

    eitangur

      Hi
      The J2EE Sun tutorial http://java.sun.com/j2ee/1.4/docs/tutorial/doc/Transaction4.html#wp80007 specifically instructs:

      Do not invoke the getRollbackOnly and setRollbackOnly methods of the EJBContext interface in bean-managed transactions. These methods should be used only in container-managed transactions. For bean-managed transactions, invoke the getStatus and rollback methods of the UserTransaction interface.


      However, the JBoss-Wiki page http://wiki.jboss.org/wiki/Wiki.jsp?page=WhatIsTheCorrectPatternForUserTransactions instructs (in the example) to use setRollbackOnly() method:
      UserTransaction ut = ... // e.g. new InitialContext().lookup("java:comp/UserTransaction");
      ut.begin();
      try
      {
      // DO SOMETHING
      }
      catch (Exception e)
      {
       ut.setRollbackOnly(); // Force a rollback for this error
       throw e;
      }
      finally
      {
       if (ut.getStatus() == Status.STATUS_ACTIVE)
       ut.commit();
       else
       ut.rollback();
      }
      


      Can anyone shed some light on this alleged contradiction?

      Thanks

        • 1. Re: contradiction between the Sun tutorial and JBoss-Wiki
          starksm64

          Simple, EJBContext != UserTransaction.

          • 2. Re: contradiction between the Sun tutorial and JBoss-Wiki
            eitangur

            From the J2EE API:

            setRollbackOnly

            public void setRollbackOnly()
            throws IllegalStateException

            Mark the current transaction for rollback. The transaction will become permanently marked for rollback. A transaction marked for rollback can never commit. Only enterprise beans with container-managed transactions are allowed to use this method.

            Throws:
            IllegalStateException - The Container throws the exception if the instance is not allowed to use this method (i.e. the instance is of a bean with bean-managed transactions).


            I'm a little confused - aren't we getting the user transaction from the SessionContext in the SessionBean? Are you saying that:
            sessionContext.setRollbackOnly();

            is different from
            UserTransaction ut = sessionContext.getUserTransaction();
            ut.setRollbackOnly();
            ?

            If so - in what way?

            Thank you very much.

            • 3. Re: contradiction between the Sun tutorial and JBoss-Wiki
              hitcher

              well, a UserTransaction is a different object than the sessioncontext.

              You can use a UserTransaction in an application outside of your EJBs (e.g. a web application) to execute several remote calls to your EJBs within one transaction context.

              • 4. Re: contradiction between the Sun tutorial and JBoss-Wiki
                hitcher

                My last post is not quite complete.
                UserTransactions are of course also use in Beans with BMT. However, the UserTransaction object is the object that you have to use to start, commit and rollback your transactions. The EJBContext holds the state for an EJB (CMT and BMT) and is managed by the AppServer, which is why you should not call setRollback on the context.