3

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)

1 Answer 1

7

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:

  1. 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.)

  2. 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.