Remember the exception resolution in spring boot in MongoDB prematurely reached end of stream

Source: Internet
Author: User
Tags findone



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


Related Article

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.