为了账号安全,请及时绑定邮箱和手机立即绑定

MongoSocketReadTimeoutException: Timeout while receiving message

MongoSocketReadTimeoutException: Timeout while receiving message

SMILET 2019-03-01 10:59:21
1.最近在使用spring-boot和用spring-data-mongo操作mongodb 2.每天早上查看服务器日志都发现报了同一个错,估计是在一整晚没人访问服务器,第二天一旦有人访问就会发生错误 3.错误日志为: 07:51:35.185 [cluster-ClusterId{value='59295e6c3532f223626a2ca0', description='null'}-localhost:27017] INFO org.mongodb.driver.cluster - Exception in monitor thread while connecting to server localhost:27017 com.mongodb.MongoSocketReadTimeoutException: Timeout while receiving message at com.mongodb.connection.InternalStreamConnection.translateReadException(InternalStreamConnection.java:477) at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:228) at com.mongodb.connection.CommandHelper.receiveReply(CommandHelper.java:134) at com.mongodb.connection.CommandHelper.receiveCommandResult(CommandHelper.java:121) at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:32) at com.mongodb.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:85) at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:45) at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:116) at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:127) at java.lang.Thread.run(Thread.java:745) Caused by: java.net.SocketTimeoutException: Read timed out at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.socketRead(SocketInputStream.java:116) at java.net.SocketInputStream.read(SocketInputStream.java:171) at java.net.SocketInputStream.read(SocketInputStream.java:141) at com.mongodb.connection.SocketStream.read(SocketStream.java:85) at com.mongodb.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:494) at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:224) ... 8 common frames omitted 07:51:31.683 [cluster-ClusterId{value='59295e583532f22362cf3b92', description='null'}-localhost:27017] INFO org.mongodb.driver.cluster - Exception in monitor thread while connecting to server localhost:27017 com.mongodb.MongoSocketReadTimeoutException: Timeout while receiving message at com.mongodb.connection.InternalStreamConnection.translateReadException(InternalStreamConnection.java:477) at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:228) at com.mongodb.connection.CommandHelper.receiveReply(CommandHelper.java:134) at com.mongodb.connection.CommandHelper.receiveCommandResult(CommandHelper.java:121) at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:32) at com.mongodb.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:85) at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:45) at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:116) at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:127) at java.lang.Thread.run(Thread.java:745) Caused by: java.net.SocketTimeoutException: Read timed out at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.socketRead(SocketInputStream.java:116) at java.net.SocketInputStream.read(SocketInputStream.java:171) at java.net.SocketInputStream.read(SocketInputStream.java:141) at com.mongodb.connection.SocketStream.read(SocketStream.java:85) at com.mongodb.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:494) at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:224) ... 8 common frames omitted 07:51:35.780 [cluster-ClusterId{value='59295e403532f2236234b535', description='null'}-localhost:27017] INFO org.mongodb.driver.cluster - Exception in monitor thread while connecting to server localhost:27017 com.mongodb.MongoSocketReadTimeoutException: Timeout while receiving message at com.mongodb.connection.InternalStreamConnection.translateReadException(InternalStreamConnection.java:477) at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:228) at com.mongodb.connection.CommandHelper.receiveReply(CommandHelper.java:134) at com.mongodb.connection.CommandHelper.receiveCommandResult(CommandHelper.java:121) at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:32) at com.mongodb.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:85) at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:45) at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:116) at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:127) at java.lang.Thread.run(Thread.java:745) Caused by: java.net.SocketTimeoutException: Read timed out at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.socketRead(SocketInputStream.java:116) at java.net.SocketInputStream.read(SocketInputStream.java:171) at java.net.SocketInputStream.read(SocketInputStream.java:141) at com.mongodb.connection.SocketStream.read(SocketStream.java:85) at com.mongodb.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:494) at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:224) ... 8 common frames omitted 08:01:39.239 [cluster-ClusterId{value='59295e583532f22362cf3b92', description='null'}-localhost:27017] INFO org.mongodb.driver.connection - Opened connection [connectionId{localValue:9, serverValue:828}] to localhost:27017 08:01:39.248 [cluster-ClusterId{value='59295e583532f22362cf3b92', description='null'}-localhost:27017] INFO org.mongodb.driver.cluster - Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 4, 4]}, minWireVersion=0, maxWireVersion=5, maxDocumentSize=16777216, roundTripTimeNanos=340319} 08:01:39.273 [cluster-ClusterId{value='59295e6c3532f223626a2ca0', description='null'}-localhost:27017] INFO org.mongodb.driver.connection - Opened connection [connectionId{localValue:7, serverValue:827}] to localhost:27017 08:01:39.274 [cluster-ClusterId{value='59295e403532f2236234b535', description='null'}-localhost:27017] INFO org.mongodb.driver.connection - Opened connection [connectionId{localValue:7, serverValue:826}] to localhost:27017 08:01:39.274 [cluster-ClusterId{value='59295e403532f2236234b535', description='null'}-localhost:27017] INFO org.mongodb.driver.cluster - Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 4, 4]}, minWireVersion=0, maxWireVersion=5, maxDocumentSize=16777216, roundTripTimeNanos=105791} 08:01:39.274 [cluster-ClusterId{value='59295e6c3532f223626a2ca0', description='null'}-localhost:27017] INFO org.mongodb.driver.cluster - Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 4, 4]}, minWireVersion=0, maxWireVersion=5, maxDocumentSize=16777216, roundTripTimeNanos=532381} 4.尝试过一些解决方法,把链接mongodb的代码改成: new MongoClient(new ServerAddress(env.getProperty("mongo.host"), Integer.valueOf(env.getProperty("mongo.port"))), Arrays.asList(credential), MongoClientOptions.builder() .socketTimeout(10000) .minHeartbeatFrequency(25) .heartbeatSocketTimeout(3000) .socketKeepAlive(true) .build()); 也把服务器(Ubuntu)ipv4的keep alive时间改成了120秒。 但是错误还在,希望大家指教指教,谢谢!
查看完整描述

3 回答

?
白衣染霜花

TA贡献1796条经验 获得超10个赞

多半是MongoDB把Java保持的连接踢掉了(状态变成CLOSE_WAIT,只能发数据但无法再收到),可以从两个地方想想办法:

  1. 把连接池配置好(这个貌似很复杂),或者干脆不用连接池,即socketKeepAlive设成false

  2. 因为你的问题都出现在早上,可以查下MongoDB有没有凌晨重置连接的策略。

查看完整回答
反对 回复 2019-03-01
?
ABOUTYOU

TA贡献1812条经验 获得超5个赞

1、可能是遇到了查询时间过长的Read,不妨看看profiling或者server log日志的信息,找到查询时间比较长的语句,例如超过10秒的语句,看是否能优化;

2、适当提高socketTimeout一些,看是否能缓解这种出错的情形。

查看完整回答
反对 回复 2019-03-01
?
jeck猫

TA贡献1909条经验 获得超7个赞

楼主解决了么?能否分享下解决方案

查看完整回答
反对 回复 2019-03-01
?
吃鸡游戏

TA贡献1829条经验 获得超7个赞

优化read时间解决了问题

查看完整回答
反对 回复 2019-03-01
  • 3 回答
  • 0 关注
  • 3241 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信