Mongo中的套接字超时异常 [英] Socket timeout exception in Mongo
问题描述
我看到由SocketTimeoutException
引起的一些MongoExceptions
(请参见下面的堆栈跟踪).也就是说,客户端未能在超时(30秒)内得到响应.现在,我想知道如何解决它.
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:
-
捕获套接字超时异常,并且您的直接请求应该能够访问新的连接池. (这是如果连接对象中的auto-reconnect选项保留为true,这是默认设置.)
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.)
使用自定义的保持活动状态,使用相同的连接池定期对mongod服务器执行ping操作,以便该池保持最新状态.
Use a custom keep-alive, to ping the mongod server periodically using the same connection pool, so the pool remains fresh.
选项1易于实现,但是默认的套接字超时约为30s,这非常过分.您可以根据需要更改此设置.
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.
选项2有点破烂,涉及线程.
Option 2 is a bit of a hack, involving threading.
这篇关于Mongo中的套接字超时异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!