[SERVER-19726] Infinite loop Created: 03/Aug/15  Updated: 03/Aug/15  Resolved: 03/Aug/15

Status: Closed
Project: Core Server
Component/s: Internal Code
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: James Scott Stanlick Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible
Operating System: ALL
Participants:

 Description   

The while (cursor.hasNext()) loops indefinitely. collection.count() == 1520

public void queryCollectionAll() {
/*NOTE
finally to ensure cursor is closed so no leaking*/
MongoCollection<Document> collection = database.getCollection("test");
MongoCursor<Document> cursor = collection.find().iterator();
long count= 0;
System.out.println(collection.count());
try {
while (cursor.hasNext())

{ count++; }

} finally

{ cursor.close(); System.out.println("collection.count():"+count); }


}



 Comments   
Comment by James Scott Stanlick [ 03/Aug/15 ]

Duh! Thanks guys

Comment by Scott Hernandez (Inactive) [ 03/Aug/15 ]

As James alluded to, you need to actually iterate the cursor by getting the next item, else your while loop will never end, since you will always have that same next one available.

Comment by James Wahlin [ 03/Aug/15 ]

Try adding a cursor.next() in your while block. You are checking whether a cursor has more data but not advancing the cursor.

Generated at Thu Feb 08 03:51:52 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.