接上一个《MONGODB01 - Prematurely reached end of stream 错误定位及修复》处理完成之后,又报错了,场景也是一段时间不访问MongoDB,突然访问的时候报此异常

 org.springframework.data.mongodb.UncategorizedMongoDbException: Exception sending message; nested exception is com.mongodb.MongoSocketWriteException: Exception sending message
at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:132)
at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:2607)
at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:2474)
at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:2282)
at org.springframework.data.mongodb.core.ExecutableFindOperationSupport$ExecutableFindSupport.doFind(ExecutableFindOperationSupport.java:213)
at org.springframework.data.mongodb.core.ExecutableFindOperationSupport$ExecutableFindSupport.all(ExecutableFindOperationSupport.java:169)
at org.springframework.data.mongodb.repository.query.AbstractMongoQuery.lambda$getExecution$1(AbstractMongoQuery.java:113)
at org.springframework.data.mongodb.repository.query.AbstractMongoQuery.execute(AbstractMongoQuery.java:97)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:602)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:590)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$Proxy185.findAllByPipelineId(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)

原因分析

经搜索,大致原因应当是连接闲置一段时间,由于防火墙或者负载均衡的原因,导致连接被关闭,而客户端并不知道,当客户端继续使用这个关闭的连接进行读写时就会出错。

问题解决

方式一:写一个配置类设置keepalive为true

顺便每个host设置最少一个连接

@Configuration
public class MongoCongig { @Bean
public MongoClientOptions mongoOptions() {
return MongoClientOptions.builder().maxConnectionIdleTime(3600000).socketKeepAlive(true).minConnectionsPerHost(1).build();
}
}

方式二:引入spring-boot-starter-mongodb-plus

POM文件

<dependency>
<groupId>com.spring4all</groupId>
<artifactId>mongodb-plus-spring-boot-starter</artifactId>
<version>1.0.0.RELEASE</version>
</dependency>

配置

spring:
data:
mongodb:
option:
max-connection-idle-time: 3600000 #空闲一个小时清理一下
socket-keep-alive: true
min-connection-per-host: 1

问题貌似解决,代码提交,部署测试,问题没有复现,好像问题的修复有个完美的结局.

BUT

但是“BUT”来了,在写MongoClientOptions的socketKeepAlive方法时看到此方法已经被@deprecated修饰,源码如下:

/**
* Sets whether socket keep-alive is enabled.
*
* @param socketKeepAlive keep-alive
* @return {@code this}
* @deprecated configuring keep-alive has been deprecated. It now defaults to true and disabling it is not recommended.
* @see <a href="https://docs.mongodb.com/manual/faq/diagnostics/#does-tcp-keepalive-time-affect-mongodb-deployments">
* Does TCP keep-alive time affect MongoDB Deployments?</a>
*/
@Deprecated
public Builder socketKeepAlive(final boolean socketKeepAlive) {
this.socketKeepAlive = socketKeepAlive;
return this;
}

官方原文:传送门

Does TCP keepalive time affect MongoDB Deployments?

If you experience network timeouts or socket errors in communication between clients and servers, or between members of a sharded cluster or replica set, check the TCP keepalive value for the affected systems.

Many operating systems set this value to 7200 seconds (two hours) by default. For MongoDB, you will generally experience better results with a shorter keepalive value, on the order of 120 seconds (two minutes).

If your MongoDB deployment experiences keepalive-related issues, you must alter the keepalive value on all affected systems. This includes all machines running mongod or mongos processes and all machines hosting client processes that connect to MongoDB.

意思你是否有网络超时的体验哈,这个是操作系统的锅(雨我无瓜),按我方式改之即可;操作系统默认的tcp_keepalive_time7200(两小时),MongoDB推荐120(两分钟)

解决方式

cat /proc/sys/net/ipv4/tcp_keepalive_time
7200
sudo sysctl -w net.ipv4.tcp_keepalive_time=<value>
#重启MongoDB后生效

上述配置在系统重启后会丢失,若要永久生效则在/etc/sysctl.conf中追加

net.ipv4.tcp_keepalive_time = <value>

重启操作系统生效

MONGODB02 - MongoSocketWriteException 异常会迟到,但从不缺席的更多相关文章

  1. 推荐系统——online(上)

    框架介绍 上一篇从总体上介绍了推荐系统,推荐系统online和offline是两个组成部分,其中offline负责数据的收集,存储,统计,模型的训练等工作:online部分负责处理用户的请求,模型数据 ...

  2. Thinking Of Matrix

    http://blog.163.com/bzm_square/blog/static/9355546320129582254842/ PS: 一种有关于矩阵的思维方法.....WiKi 向量空间,不定 ...

  3. android webview 漏洞背后的节操

    by superhei 2013/09/06 [注:本文提到的都是我个人的观点,该行为也是私人行为,与任何组织.公司无关.另:水军请自重!] 一.前言   这两天,一个2+年前的android web ...

  4. 关于矩阵最通俗的解释-超级经典zz

    线性代数课程,无论你从行列式入手还是直接从矩阵入手,从一开始就充斥着莫名其妙.比如说,在全国一般工科院系教学中应用最广泛的同济线性代数教材(现在到了第四版),一上来就介绍逆序数这个“前无古人,后无来者 ...

  5. 【转】关于Sentry

    1. Sentry介绍及使用 Sentry 是一个实时事件日志记录和汇集的平台.其专注于错误监控以及提取一切事后处理所需信息而不依赖于麻烦的用户反馈. 备注:国内有同类型的产品Fundebug,提供网 ...

  6. 关于Sentry(转)

    原文:http://blog.csdn.net/largetalk/article/details/8640854 1. Sentry介绍及使用 Sentry is a realtime event ...

  7. YFI币之后,BGV能否主宰DeFi 沉浮?

    回望今年,币圈风起云涌,比特币.YFI.BGV等一众数字货币共同打造了火热的币圈景象,在短短的时间里可以说是又形成了新的生态,业内对于BGV等新币种的认可度也达到了新高.2020已经接近尾声,放眼20 ...

  8. BGV暴涨千倍,未来或将超越YFI领跑DeFi全场!

    毫无疑问,YFI在2020年上半年以一己之力掀翻了DeFi市场的热潮.迄今为止,YFI的新鲜资讯从不缺席,最近也是频频登上各大知名媒体热搜.其币价远远超过比特币价格,也让资本市场注意到DeFi市场原来 ...

  9. 开发数学系统时,需要掌握的几个基于Web的数学框架

    在做数学系统时,经常要和数学公式打交道,这里介绍几个常用的基于Web的数学处理软件. 数学系统主要包括三类:(1)数学公式的显示,也就是如何使用web显示复杂的数学公式. (2)图像制作,例如长方形, ...

随机推荐

  1. 详细分析 Java 中启动线程的正确和错误方式

    目录 启动线程的正确和错误方式 前文回顾 start 方法和 run 方法的比较 start 方法分析 start 方法的含义以及注意事项 start 方法源码分析 源码 源码中的流程 run 方法分 ...

  2. vue+less换肤,主题切换方案

    新的项目对于客户自定义要求很高,然后换肤是其中一个很小的模块,经过了一段时间的摸索,看了许多文章,找到了几种方案. https://www.cnblogs.com/leiting/p/11203383 ...

  3. Java date format 时间格式化

      import java.util.Date; import java.text.DateFormat; /** * 格式化时间类 * DateFormat.FULL = 0 * DateForma ...

  4. C++有子对象的派生类的构造函数

    转载:https://blog.csdn.net/qq1169091731/article/details/50934588?utm_source=blogxgwz6 类的数据成员不但可以是标准型(如 ...

  5. Visual Studio中Debug与Release以及x86、x64、Any CPU的区别 &&&& VS中Debug与Release、_WIN32与_WIN64的区别

    本以为这些无关紧要的 Debug与Release以及x86.x64.Any CPU 差点搞死人了. 看了以下博文才后怕,难怪我切换了一下模式,程序就pass了.... 转载: 1.https://ww ...

  6. 别人写的很好Arduino教材

    原文来自:https://www.arduino.cn/thread-31720-1-1.html 上一篇:Arduino教程--通过 库管理器 添加库 http://www.arduino.cn/t ...

  7. P2783 有机化学之神偶尔也会作弊 题解

    题面 前言 这道题以前还是道(水)黑题,现在怎么降紫了???? 前置芝士 tarjain 缩点 倍增求LCA或树剖求LCA 脑子... 题意 给你一个无向图,要求你把所有的环缩成一个点.在新得到的图上 ...

  8. Tensorflow学习笔记No.4.2

    使用CNN卷积神经网络(2) 使用Tensorflow搭建简单的CNN卷积神经网络对fashion_mnist数据集进行分类 不了解是那么是CNN卷积神经网络的小伙伴可以参考上一篇博客(Tensorf ...

  9. 洛谷 P1903 [国家集训队]数颜色 / 维护队列 带修莫队

    题目描述 墨墨购买了一套\(N\)支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会向你发布如下指令: \(1\). \(Q\) \(L\) \(R\)代表询问你从第\(L\) ...

  10. golang执行exec命令

    创建对象: cmd, err := exec.Command("echo", "show me")   执行命令: cmd.Run()  //Run 阻塞进程, ...