Remember the exception resolution in spring boot in MongoDB prematurely reached end of stream
Source: Internet
Author: User
findone
Developer on Alibaba Coud: Build your first app with APIs, SDKs, and tutorials on the Alibaba Cloud.
Read more >
MongoDB is used in the Spring boot project, and when MongoDB is not manipulated for a while, the next time you manipulate MongoDB there will be an exception. Exceptions are as follows:
org.springframework.data.mongodb.UncategorizedMongoDbException: Prematurely reached end of stream; nested exception is com.mongodb.MongoSocketReadException: Prematurely reached end of stream
at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:107)
at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:2146)
at org.springframework.data.mongodb.core.MongoTemplate.executeFindOneInternal(MongoTemplate.java:1928)
at org.springframework.data.mongodb.core.MongoTemplate.doFindOne(MongoTemplate.java:1736)
at org.springframework.data.mongodb.core.MongoTemplate.findOne(MongoTemplate.java:606)
at org.springframework.data.mongodb.core.MongoTemplate.findOne(MongoTemplate.java:601)
at com.lwli.service.impl.SessionServiceImpl.findOneSessionByCustomerIdAndStatus(SessionServiceImpl.java:51) at java.lang.Thread.run(Thread.java:745)
Caused by: com.mongodb.MongoSocketReadException: Prematurely reached end of stream
at com.mongodb.connection.SocketStream.read(SocketStream.java:88)
at com.mongodb.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:494)
at com.mongodb.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:224)
at com.mongodb.connection.UsageTrackingInternalConnection.receiveMessage(UsageTrackingInternalConnection.java:96)
at com.mongodb.connection.DefaultConnectionPool$PooledConnection.receiveMessage(DefaultConnectionPool.java:440)
at com.mongodb.connection.QueryProtocol.execute(QueryProtocol.java:289)
at com.mongodb.connection.QueryProtocol.execute(QueryProtocol.java:54)
at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:168)
at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:289)
at com.mongodb.connection.DefaultServerConnection.query(DefaultServerConnection.java:212)
at com.mongodb.operation.FindOperation$1.call(FindOperation.java:525)
at com.mongodb.operation.FindOperation$1.call(FindOperation.java:510)
at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:435)
at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:408)
at com.mongodb.operation.FindOperation.execute(FindOperation.java:510)
at com.mongodb.operation.FindOperation.execute(FindOperation.java:81)
at com.mongodb.Mongo.execute(Mongo.java:836)
at com.mongodb.Mongo$2.execute(Mongo.java:823)
at com.mongodb.DBCursor.initializeCursor(DBCursor.java:870)
at com.mongodb.DBCursor.hasNext(DBCursor.java:142)
at com.mongodb.DBCursor.one(DBCursor.java:679)
at com.mongodb.DBCollection.findOne(DBCollection.java:833)
at com.mongodb.DBCollection.findOne(DBCollection.java:796)
at com.mongodb.DBCollection.findOne(DBCollection.java:743)
at org.springframework.data.mongodb.core.MongoTemplate$FindOneCallback.doInCollection(MongoTemplate.java:2197)
at org.springframework.data.mongodb.core.MongoTemplate$FindOneCallback.doInCollection(MongoTemplate.java:2181)
at org.springframework.data.mongodb.core.MongoTemplate.executeFindOneInternal(MongoTemplate.java:1925)
... 12 more
Online search, the problem is a lot of reasons, the main reason may have a connection timeout, read and write timeout, according to other people's tips, set up
Socketkeepalive = true;
Sockettimeout = 30000;
However, there is no effect, and when the mongdb is visited again over a period of time, the exception occurs. When searching for Google, an article was found https://studio3t.com/whats-new/ How-to-prevent-your-connection-from-dropping-with-hosted-mongodb-instances/, mentioned above, when the connection is idle for a period of time, due to firewall or load balancing reasons, Causes the connection to be closed and the client does not know that an error occurs when the client continues to read and write with the closed connection.
The solution is to set the connection idle time, when more than this idle time the client actively shut down the connection, the next time the connection is re-established, this can effectively avoid the problem of connection failure.
Configure the Mongoclientoptions bean in spring boot. For example, set the maximum idle time to 6000ms.
@Configuration public class WechatMpConfiguration {
@Bean public MongoClientOptions mongoOptions() {
After the above settings, there is no more prematurely reached end of stream exception later.
Remember the exception resolution in spring boot in MongoDB prematurely reached end of stream
This article is an English version of an article which is originally in the Chinese language on aliyun.com and is provided for information purposes only. This website makes no representation or warranty of any kind, either expressed or implied, as to the accuracy, completeness ownership or
reliability of the article or any translations thereof. If you have any concerns or complaints relating to the article, please send an email, providing a detailed description of the concern or
complaint, to [email protected]. A staff member will contact you within 5 working days. Once verified, infringing content will be removed immediately.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
Start building with 50+ products and up to 12 months usage for Elastic Compute Service
Get Started for Free//intl.aliyun.com/pricing//intl.aliyun.com/trust-center//intl.aliyun.com/free-trial/enterprise//intl.aliyun.com/startup//intl.aliyun.com/partner