接上一个《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. jdbc原理与步骤

    jdbc原理 1.加载JDBC驱动,并将其注册到DriverManager 2.建立数据库连接,获取connection对象 3.建立Statement对象或PreparedStatement对象 4 ...

  2. mybatis基础使用

    MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis .20 ...

  3. Python练习题 042:Project Euler 014:最长的考拉兹序列

    本题来自 Project Euler 第14题:https://projecteuler.net/problem=14 ''' Project Euler: Problem 14: Longest C ...

  4. pytorch和tensorflow的爱恨情仇之张量

    pytorch和tensorflow的爱恨情仇之基本数据类型:https://www.cnblogs.com/xiximayou/p/13759451.html pytorch版本:1.6.0 ten ...

  5. spring-boot-route(九)整合JPA操作数据库

    单调的增删改查让越来越多的程序员感到乏味,这时候就出现了很多优秀的框架,完成了对增删改查操作的封装,只需要简单配置,无需书写任何sql,就可以完成增删改查.这里比较推荐的是Spring Data Jp ...

  6. P3431 [POI2005]AUT-The Bus

    Link 简化题意: 给你一张网格图,每个点有其对应的权值,让你找出来一条横纵坐标都单调不降的路径,并最大化经过点的权值. 分析: 这是经典的二维数点或者二维偏序问题. 如果两维一直在变的话,我们不是 ...

  7. Linux 安装Navicat Premium 15

    参考:https://gitee.com/andisolo/navicat-keygen 安装 aptitude 管理软件 $ sudo apt-get install aptitude 安装Navi ...

  8. Linux 杀毒软件ClamAV安装部署

    环境说明 系统安全需求,批量安装免费杀毒软件: 操作系统统一为CentOS 7 x64,在此选择免费开源杀毒软件ClamAV: 两种安装方式 1.yum 安装: 2.源码包编译安装: 安装参考网址: ...

  9. 利用 Python 插入 Oracle 数据

    # coding=utf-8 ''''' Created on 2020-01-05 @author: Mr. Zheng ''' import json; import urllib2 import ...

  10. python反序列化学习记录

    pickle与序列化和反序列化 官方文档 模块 pickle 实现了对一个 Python 对象结构的二进制序列化和反序列化. "pickling" 是将 Python 对象及其所拥 ...