接上一个《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. Spring Environment对象获取属性

    String[] activeProfiles = env.getActiveProfiles();//获取当前是启用哪一个个配置文件 System.out.println(Arrays.toStri ...

  2. Ajax接收int类型乱码

    在Ajax返回值类型是 "text" 的时候,接收int类型时可能会出现ၧ 解决方法:将int转为String即可 int money =100; String s = Integ ...

  3. MySQL的8小时连接超时时间,导致系统过夜即崩溃,报错Could not roll back Hibernate transaction

    2014年3月开始给单位开发<机关规范化管理网络平台>,10月底成功上线运行,但是存在一个bug: 部署环境: apache tomcat 6.0.41 + mysql5.5 + jbpm ...

  4. 048 01 Android 零基础入门 01 Java基础语法 05 Java流程控制之循环结构 10 案例——阶乘的累加和

    048 01 Android 零基础入门 01 Java基础语法 05 Java流程控制之循环结构 10 案例--阶乘的累加和 本文知识点:通过案例练习嵌套循环应用 案例练习--阶乘的累加和 案例题目 ...

  5. C语言中time_t数据类型详细介绍

    包含文件:<time.h> #ifndef __TIME_T #define __TIME_T     /* 避免重复定义 time_t */ typedef long     time_ ...

  6. P5911 [POI2004]PRZ (状态压缩dp+枚举子集)

    题目背景 一只队伍在爬山时碰到了雪崩,他们在逃跑时遇到了一座桥,他们要尽快的过桥. 题目描述 桥已经很旧了, 所以它不能承受太重的东西.任何时候队伍在桥上的人都不能超过一定的限制. 所以这只队伍过桥时 ...

  7. 【题解】小Z的袜子

    期末考试结束了,来写写blog吧 题目描述 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命-- 具 ...

  8. Jmeter接口测试--上传附件

    jmeter接口测试上传附件指引 1.添加HTTP请求取样器--在取样器中的HTTP请求项中对"使用KeepAlive"."对POST使用multipart/form-d ...

  9. 推荐Java字节码解析工具classpy

    Classpy Classpy is a GUI tool for investigating Java class file, Lua binary chunk, Wasm binary code, ...

  10. C#开启线程的四种方式

    如果需要查看更多文章,请微信搜索公众号 csharp编程大全,需要进C#交流群群请加微信z438679770,备注进群, 我邀请你进群! ! ! 1.异步委托开启线程 public class Pro ...