[JAVA-764] Facing a 'no more' RuntimeException on DBCursor#next() when DBCursor#count() == 1 Created: 20/Feb/13  Updated: 20/Feb/13  Resolved: 20/Feb/13

Status: Closed
Project: Java Driver
Component/s: API
Affects Version/s: 2.9.3
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Brice Laurencin Assignee: Unassigned
Resolution: Done Votes: 0
Labels: cursors, java
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

$ uname -a
Linux 3.5.0-24-generic #37-Ubuntu SMP Thu Feb 7 01:50:30 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
$ mongo --version
MongoDB shell version: 2.0.6
$ mongod --version
db version v2.0.6, pdfile version 4.5
Tue Feb 12 18:53:04 git version: nogitversion
$ java -version
java version "1.7.0_13"
Java(TM) SE Runtime Environment (build 1.7.0_13-b20)
Java HotSpot(TM) 64-Bit Server VM (build 23.7-b01, mixed mode)



 Description   

Hello,

I am reading data from mongo in Java, with this code:

DBObject dbo = new BasicDBObject(LOCKED_DOC_ID_FIELD, gdto.getDocId());
DBCursor lock = locks.find(dbo);
if (lock.count() == 1) {
	DBObject lockDBO = lock.next();
	long createdAtTS = (Long) lockDBO.get(CREATED_AT_FIELD);
	Date d = new Date(createdAtTS);
	return new Lock(gdto, d);
} else if (lock.count() > 1) {
	throw new IllegalStateException("It seems like mongoDB locks.locks collection does not have a unique index on lockedId. Do: use locks; db.locks.ensureIndex({lockedDocId:1}, {unique:true})");
			}

I somtimes get an Exception on `DBObject lockDBO = lock.next();`:

java.lang.RuntimeException: no more
at com.mongodb.DBApiLayer$Result.next(DBApiLayer.java:388)
at com.mongodb.DBApiLayer$Result.next(DBApiLayer.java:350)
at com.mongodb.DBCursor._next(DBCursor.java:421)
at com.mongodb.DBCursor.next(DBCursor.java:494)

How can this be?

Thanks for your precious help.
Brice.



 Comments   
Comment by Jeffrey Yemin [ 20/Feb/13 ]

Also, I'm not sure what the intention of this code is, but if you're using a document as a mutex of some sort, you may need to mutate it atomically so that only one client can hold the lock. For that, you can look at http://docs.mongodb.org/manual/reference/command/findAndModify/.

Comment by Scott Hernandez (Inactive) [ 20/Feb/13 ]

The find, and count methods are two separate server calls. Instead of using count() == 1 check that lock.hasNext() is true or do a findOne.

Generated at Thu Feb 08 08:53:01 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.