I see a few MongoExceptions caused by SocketTimeoutException (see the stack trace below). That is, client failed to get a response within the timeout (30 sec.). Now I wonder how to fix it.
The trivial solution is to increase the timeout but I'd like to understand the root cause first. What would you suggest?
caused by java.net.SocketTimeoutException: Read timed out
java.net.SocketInputStream.socketRead0 (Native Method)
java.net.SocketInputStream.read (SocketInputStream.java:152)
java.net.SocketInputStream.read (SocketInputStream.java:122)
java.io.BufferedInputStream.read1 (BufferedInputStream.java:273)
java.io.BufferedInputStream.read (BufferedInputStream.java:334)
com.mongodb.Response$MyInputStream.read (Response.java:168)
org.bson.BasicBSONDecoder$BSONInput.fill (BasicBSONDecoder.java:386)
org.bson.BasicBSONDecoder$BSONInput.readUTF8String (BasicBSONDecoder.java:460)
org.bson.BasicBSONDecoder.decodeElement (BasicBSONDecoder.java:155)
org.bson.BasicBSONDecoder._decode (BasicBSONDecoder.java:79)
org.bson.BasicBSONDecoder.decode (BasicBSONDecoder.java:57)
com.mongodb.DefaultDBDecoder.decode (DefaultDBDecoder.java:61)
com.mongodb.Response. (Response.java:83)
com.mongodb.DBPort.go (DBPort.java:142)
com.mongodb.DBPort.call (DBPort.java:92)
com.mongodb.DBTCPConnector.innerCall (DBTCPConnector.java:244)
com.mongodb.DBTCPConnector.call (DBTCPConnector.java:216)
com.mongodb.DBApiLayer$MyCollection.__find (DBApiLayer.java:288)
com.mongodb.DBApiLayer$MyCollection.__find (DBApiLayer.java:273)
com.mongodb.DBCollection.findOne (DBCollection.java:728)
com.mongodb.DBCollection.findOne (DBCollection.java:708)
Did these time-outs happen after periods of inactivity? It's likely that your connection pool became stale after remaining idle for too long.
If so, there are two ways of going about this:
Catch the socket time-out exceptions, and your immediate request should be able to access a fresh connection pool. (this is if the auto-reconnect option in your connection object is left to true, which is default.)
Use a custom keep-alive, to ping the mongod server periodically using the same connection pool, so the pool remains fresh.
Option 1 is easy to implement, but the default socket time-out is around 30s which is rather excessive. You can change this setting as required.
Option 2 is a bit of a hack, involving threading.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With