1 Reply Latest reply on Mar 2, 2004 8:33 PM by sesques

    Error: CMR Collection only used within transaction

    luigifonti

      I've developed a very simple CMP EJB-set with multiple relations between them.
      Testing them, I call an EJB method returning a Collection: the CurrentCount(s) owned by a given Customer, then I try to print them.

      I get the following obscure error message:

      CMR Collection may only be used within the transaction in which it was created.

      What does it mean?
      Luigi Fonti

        • 1. Re: Error: CMR Collection only used within transaction
          sesques

          Hi,

          Effectively, a collection returned by a getter on a CMR field can only be used within the initial transation (created during the EJB getter call).
          You should set the transaction type to "required" for your getter method, and test your code within this enveloppe:

          UserTransaction tx = null;
          InitialContext ctx = null;
          boolean rollback = false;
          try {
          ctx = new InitialContext();
          tx = (UserTransaction)ctx.lookup("UserTransaction");
          tx.begin();
          // Do your thing
          } catch (Throwable ex) {
          // Maybe set rollback = true if needed
          } finally {
          try {
          // Close the InitialContext
          if (ctx != null) {
          ctx.close();
          }
          // Must always remember to commit or rollback.
          if (tx != null) {
          if (rollback) {
          tx.rollback();
          } else {
          tx.commit();
          }
          }
          } catch (Throwable ex2) {
          // Big problem if falls there !
          }
          }