2 Replies Latest reply on Oct 27, 2010 2:46 PM by Mauro Flores

    CMT - getTransaction

    Mauro Flores
      How can I get the instance of the transaction from a CMT EJB method?
      Something like in BMT: context.getUserTransaction().
      I know I can't change the properties of a CMT transaction, but I would like to display those properties.
      For instance I would like to see the ID of the transaction in order to compare to the IDs shown in the log file.

        • 1. Re: CMT - getTransaction
          René van Wijk
          You can inject an EjbContext into your session or messagedrivenbean.
          From this context you can call ejbContext.getUserTransaction()

          Note from the Javadocs that you are only allowed to use this
          if your enterprise bean is using bean managed transactions.

          You can also lookup an instance of UserTransaction, for example,
          Context context = null;
          try {
               context = new InitialContext();
               userTransation = (UserTransaction)context.lookup("java:comp/UserTransaction");
          } catch (NamingException e) {
          • 2. Re: CMT - getTransaction
            Mauro Flores
            Thanks, René.

            The lookup worked fine in a Session Bean (EJB3) with CONTAINER Managed Transaction.

            @Stateless(mappedName = "MyInterface")
            public class MyEJB implements MyInterface{
            ....SessionContext context;
            ...public void myMethod() {
            ......ClientTransactionManagerImpl userTransation = (ClientTransactionManagerImpl)context.lookup("java:comp/UserTransaction");
            ......System.out.println("Transaction: " + userTransation.getTransactionKey());