添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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() {
 return MongoClientOptions.builder().maxConnectionIdleTime(6000).build();

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.

Related Keywords: redis cache in spring boot spring boot mongodb query spring boot mongodb example seems to have reached end of line spring boot mongodb crud example spring boot starter data mongodb end of file in c Related Article The maximum number of collections that can be used in MongoDB The download and installation of MongoDB MongoDB replica set IDs do not match MongoDB Error: Connection refused because too many open conne... How to install MongoDB and error 1053 in windows

Contact Us

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.

If you find any instances of plagiarism from the community, please send an email to: [email protected] and provide relevant evidence. A staff member will contact you within 5 working days. Remember the exception resolution in spring boot in MongoDB prematurely reached end of stream MongoDB Error: Connection refused because too many open connections:819 How MongoDB implements Backup compression Install MongoDB on bluehost A summary of the common issues in deployment and maintenance management of MongoDB shards About MongoDB and SQL Server using the self-brought tools to realize the rapid migration of data and considerations microsoft download center down wordpress address url site address url wordpress address url windows installer 4 0 download 302 not found web address url definition site address url wordpress db2 integer mac os installation step by step pdf abbreviation for return

A Free Trial That Lets You Build Big!

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