1 Reply Latest reply on Dec 19, 2003 3:07 AM by vladimir_l

    How may I use CMR collections?

      Hi everybody,

      Can anybody explain to me how to use CMR collections outside of EJB scope, but still inside the J2EE server?

      I have business model represented by EJB with CMR (one-to-one, one-to-many and many-to-many relationships). All of them are with local interfaces. I'm using this model inside the web-application (based upon Struts framework). I don't have any problem to use EJB until I come to the point of usage of CMR collections. The following code causes "java.lang.IllegalStateException: A CMR collection may only be used within the transction in which it was created":


      //...

      GroupLocalHome groupHome = GroupUtil.getLocalHome();
      GroupLocal group = groupHome.findByPrimaryKey(groupId);

      Collection roles = group.getRoles();
      Iterator roleIterator = roles.iterator();
      while (roleIterator.hasNext()) {
      RoleLocal role = (RoleLocal) roleIterator.next();
      // ...
      }


      I was desperately looking through internet and found only couple of references to my problem. The most remarkable is http://www.mail-archive.com/jboss-user@lists.sourceforge.net/msg33635.html//www.mail-archive.com/jboss-user@lists.sourceforge.net/msg33635.html. However, I didn't manage to get it working with JBoss. JBoss was complaining that javax/jtl/UserTransaction isn't found or so...

      Thanks a lot in advance!

        • 1. Re: How may I use CMR collections?

          Sorry guys!

          My bad! I found where I was wrong. The UserTransaction must be javax.transaction.UserTransaction not a javax.jts.UserTransaction. This one came from WebLogic jar file that was in my classpath and seems to have nothing to do with Java standards and JBoss implementation.

          So the following changes fix my problem:

          import javax.transaction.UserTransaction;

          //...
          InitialContext ctx = new InitialContext();
          UserTransaction ut = (UserTransaction)ctx.lookup("java:comp/UserTransaction");

          GroupLocalHome groupHome = GroupUtil.getLocalHome();
          GroupLocal group = groupHome.findByPrimaryKey(groupId);

          ut.begin();
          Collection roles = group.getRoles();
          Iterator roleIterator = roles.iterator();
          while (roleIterator.hasNext()) {
          RoleLocal role = (RoleLocal) roleIterator.next();
          // ...
          }
          ut.commit();
          // ...
          ctx.close();



          Thanks for your attention :)