Transfer: get{OneToMany|ManyToMany}Iterator() Never-Ending Loop

Posted on July 11, 2008 at 7:11 AM in ColdFusion, Transfer

When using Transfer in your application, you'll often have a One-to-Many (o2m) or a Many-to-Many (m2m) relationship in your Transfer config file. If you review the generated methods docs, you will see that you can grab an iterator with which to loop over the object collection. When you do, don't be a dweeb like me and create a never-ending loop.

Look carefully at the following two snippets of code.

Sample 1:

  1. <cfscript>
  2. var i = 0;
  3. var temp = "";
  4. var iterator = getEmailIterator();
  5. while ( iterator.hasNext() ) {
  6. temp = iterator.next();
  7. i = i+1;
  8. }
  9. return i;
  10. </cfscript>

Sample 2:

  1. <cfscript>
  2. var i = 0;
  3. var temp = "";
  4. while ( getEmailIterator().hasNext() ) {
  5. temp = getEmailIterator().next();
  6. i = i+1;
  7. }
  8. return i;
  9. </cfscript>

See the difference in them? It's rather subtle, but it'll bring your processor to its knees if you choose the wrong one. Which one is right? Which one is wrong?

This post is probably of very little benefit to anyone else. Honestly, I decided to write it while it was fresh on my mind so that (a) hopefully I will remember it better [since I wrote it down], and (b) so I can refer back to it the next time I forget again and cripple my server with the wrong version... again. (Yeah, I did. You can quit laughing now.)

"Well, which damn one is it, Quack?"

Oh, yeah. Sorry, I forgot about that. If you chose Sample 1 as the correct one, you owe yourself a pat on the back. If you chose Sample 2 as the correct one, welcome to my world, my friend!

Comments
(Comment Moderation is enabled. Your comment will not appear until approved.)

On 5/29/09 at 3:46 PM, pedro said:

uhm... At first I thought both should behave the same way, as I assumed that the variable "iterator" was a reference to the iterator instance... I guess not... and it's actually very clear from the documentation you pointed to. The get...Iterator method returns a java.util.Iterator: "...creates a Iterator on a shallow copy of the collection."

Thanks for the post. Insteresting!

Pedro.
CodeBassRadio

Latest Articles

Eventually something really brilliant and witty will appear right here.

Calendar

May 2022
S M T W T F S
« Apr  
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        

Subscribe

Enter a valid email address.

The Obligatory Wish List