Get the App
SLTechnology News&Howtos  ›  Database  › 

How to solve the query timeout exception in MongoDB

Shulou Source: shulou.com Published: 2022-05-31 19:33:24 10月31日 Update

MongoDB query timeout exception how to solve, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Aggregating a collection of more than a million records.

DBObject match= (DBObject) JSON.parse ("{$match: {logType: {'$in': [5 sum':1 9]}}"); DBObject group= (DBObject) JSON.parse ("{$group: {'_ id':'$domainUrl','count': {'$sum':1}}"); AggregationOutput output = logCollection.aggregate (match,group)

Occasional Read timed out exceptions occur.

Com.mongodb.MongoException$Network: Read operation to server / 192.168.10.202 failed on database adLogTable at com.mongodb.DBTCPConnector.innerCall (DBTCPConnector.java:253) at com.mongodb.DB.command (DB.java:261) at com.mongodb.DB.command (DB.java:243). Caused by: java.net.SocketTimeoutException: Read timed out at java.net.SocketInputStream.socketRead0 (Native Method) at java.net.SocketInputStream.read (SocketInputStream.java:152)

After many tests, it is found that the average time of performing an aggregation is 5s, and an error will be reported when it exceeds 5s!

Then view the configuration information of MongoDB:

Socket-timeout= "5000" / / 5s

The default configuration of socket-timeout is 0, which means there is no limit.

There is no time-out limit, and the problem with the system is not easy to find. A reasonable time-out should be given according to the actual situation.

After several tests, it was found that the maximum execution time was 6 seconds, and the timeout time was set to 10000.

Socket-timeout= "10000" / / 10s

Note: when MongoDB integrates with Spring, if you want to configure multiple MongDB sources, only one configuration will be enabled.

Parameter configuration information should be stored in the properties file.

Get configuration parameters through Java API

DBCollection logCollection = mongoTemplate.getCollection (collName); MongoOptions mongoOptions = logCollection.getDB (). GetMongo (). GetMongoOptions (); System.out.println (mongoOptions.getSocketTimeout ())

* one thing: the difference between ConnectionTimeOut and SocketTimeOut:

A complete request consists of three phases:

Establish a connection

Data transmission

Disconnect

If the request to establish a connection with the server (in this case, the database) takes longer than ConnectionTimeOut, ConnectionTimeOutException will be thrown, that is, the server connection timed out and the connection was not established within the specified time.

If the connection to the server is successful, the data transfer begins.

If the server takes too long to process the data and exceeds the SocketTimeOut, the SocketTimeOutExceptin will be thrown, that is, the server response timed out and the server did not return the data to the client within the specified time.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Time server service configuration data information parameters data transmission transmission help test limit query longest clear success ten thousand three that is content Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi MySQL Docker Xiaomi Shulou Information