4

I am using MongoDb and I am facing problems while reading the records from the DB. I am able to get them in the cursor but when I try to get the records from the cursor using cursor.hasNext() it gives me following exception:

com.mongodb.MongoInternalException: couldn't get next element
        at com.mongodb.DBCursor.hasNext(DBCursor.java:459)
Caused by: java.net.SocketTimeoutException: Read timed out
        at java.net.SocketInputStream.socketRead0(Native Method)
        at java.net.SocketInputStream.read(SocketInputStream.java:146)
        at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
        at java.io.BufferedInputStream.read1(BufferedInputStream.java:275)
        at java.io.BufferedInputStream.read(BufferedInputStream.java:334)
        at org.bson.io.Bits.readFully(Bits.java:35)
        at org.bson.io.Bits.readFully(Bits.java:28)
        at com.mongodb.Response.<init>(Response.java:35)
        at com.mongodb.DBPort.go(DBPort.java:101)
        at com.mongodb.DBPort.go(DBPort.java:66)
        at com.mongodb.DBPort.call(DBPort.java:56)
        at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:211)
        at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:220)
        at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:220)
        at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:266)
        at com.mongodb.DBCursor._check(DBCursor.java:309)
        at com.mongodb.DBCursor._hasNext(DBCursor.java:431)
        at com.mongodb.DBCursor.hasNext(DBCursor.java:456)

Maybe I am facing this issue as my data is continously increasing so I am getting more number of records in the cursor. Also the database I am accessing is on remote machine.

Please need help on this.

Thanks!

Parvin Gasimzade
  • 25,180
  • 8
  • 56
  • 83
azhar_salati
  • 1,554
  • 5
  • 28
  • 54
  • are you able to retrieve any records? do you have firewall rules in place that might be causing this problem? Have you timed how long it takes before getting the timeout? – NG. Mar 29 '11 at 12:06
  • Can you paste the code you're using that generates this error? How does it compare to the standard tutorial? Are you getting some documents before this error happens or does it happen *before* you get your first document back? – Gates VP Mar 30 '11 at 21:29
  • Can you make sure there isn't a firewall or something similar between you and your server? This looks like a case of a network port being closed due to timeout by an overaggressive network member. – Brendan W. McAdams Mar 30 '11 at 23:52
  • Yes I am able to retrieve the records, so this might not be the issue related to the firewall because if it was then I wouldn't have got the records at all. Also I have set the socket timeout to 2000 ms and connection time out to 2000 ms. Can this issue occur because of the java driver and mongodb version compatability.(I am using mongodb 1.6.5 and mongo+java driver version is 2.4) – azhar_salati Mar 31 '11 at 07:48

1 Answers1

4

Based on what you've described, I think I've experienced this issue in PHP when the collection is experiencing both heavy read load and write load. Some reads might work but eventually they'll start timing out. My cursor timeout is set to 30s, which isn't an issue since we use Mongo for backend data mining/processing. We've been able to mitigate the issue somewhat by sharding our servers, but the issue still crops up fairly regularly. I think this stems from the fact that most of Mongo is single-threaded and so a heavy load turns into a long processing queue which eventually turns into timeouts.

I'd also check and make sure your RAM isn't getting filled up by index data or actual data - if that's the case, Mongo has to go to the hard drive to get that data and that is over 80x slower than reading from memory. You can see what your index/data footprint is by running db.getStats() for the database in question.

Jimmy Sawczuk
  • 13,488
  • 7
  • 46
  • 60