For Option 2, you'd do something like:

  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
  } 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) {
        // Whoa Nelly
     }
  }

Anyway, the important things I've found is that you really *must* 
ensure you either commit or rollback the transaction because if you 
forget, really bad things happen. Also closing the InitialContext 
used to be pretty important, and probably still is though I think it 
probably does take care of itself eventually.

That'll clear up the error you have, if you put your code where I 
marked 'Do your thing'.

-Neal

On November 17, 2003 10:59 pm, Alwyn Schoeman wrote:
> Hi,
>
> As I understand it you have to access the Collection in the same
> transaction as in which it gets created.
>
> 2 options I know of available to you is:
> 1) Return collection of value objects and iterate over those.
> 2) Do the CMR access in a UserTransaction.
>
> I did the 1st one as I'm not that familiar with 2.
>
> On Fri, Nov 14, 2003 at 04:14:59PM -0800, Balakrishnan, Vijay wrote:
> > http://www.mail-archive.com/[EMAIL PROTECTED]/msg2
> >2637.html
> >
> > Hi,
> >
> > I am getting a very similar error to the thread above:
> > java.lang.IllegalStateException: A CMR collection may only be
> > used within the transction in which it was created
> >     at
> > org.jboss.ejb.plugins.cmp.jdbc.bridge.RelationSet.getIdList(Unkno
> >wn Source)
> >
> > I have a Message driven bean with Commit Option A and onMessage
> > method with a transaction attribute set to "Required" as follows:
> > * @ejb.transaction type="Required"
> >
> >
> > Here is the relevant code snippet from the onMessage method:
> >
> > ItemLocal itemLocal = cpeLocal.getItemLocal();
> > Collection cpes = itemLocal.getCpesLocal();
> >
> > Iterator iterator = cpes.iterator(); <==== fails here with the
> > exception above
> >
> >
> > Thanks in advance,
> > Vijay
> >
> >
> >
> > -------------------------------------------------------
> > This SF. Net email is sponsored by: GoToMyPC
> > GoToMyPC is the fast, easy and secure way to access your computer
> > from any Web browser or wireless device. Click here to Try it
> > Free!
> > https://www.gotomypc.com/tr/OSDN/AW/Q4_2003/t/g22lp?Target=mm/g22
> >lp.tmpl _______________________________________________
> > JBoss-user mailing list
> > [EMAIL PROTECTED]
> > https://lists.sourceforge.net/lists/listinfo/jboss-user



-------------------------------------------------------
This SF. Net email is sponsored by: GoToMyPC
GoToMyPC is the fast, easy and secure way to access your computer from
any Web browser or wireless device. Click here to Try it Free!
https://www.gotomypc.com/tr/OSDN/AW/Q4_2003/t/g22lp?Target=mm/g22lp.tmpl
_______________________________________________
JBoss-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-user

Reply via email to