Causes and solutions for MongoDB query timeout exceptions

Source: Internet
Author: User
Tags mongodb query

Causes and solutions for MongoDB query timeout exceptions

Aggregate a collection of more than one million records.

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

Occasionally, a Read timed out exception occurs.

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

After multiple tests, it is found that the average time for performing an aggregation operation is 5 s. If the time exceeds 5 s, an error is reported!

Then, view the MongoDB configuration information:

 
 
  1. socket-timeout="5000" //5s 

The default socket-timeout configuration is 0, that is, there is no limit.

There is no timeout limit, and it is not easy to find out a problem in the system. A reasonable timeout time should be provided according to the actual situation.

Multiple tests show that the maximum execution time is 6 seconds, and the timeout time is set to 10000.

 
 
  1. socket-timeout="10000" //10s 

Note: When MongoDB is integrated with Spring, if you want to configure multiple MongDB sources, only the last <mongo: options> configuration is enabled.

The parameter configuration information should be stored in the properties file.

 
 
  1. <mongo:mongo host="${mongodb.ip}" id="mongo202" port="${mongodb.port}"> 
  2. <mongo:options connections-per-host="200" 
  3. threads-allowed-to-block-for-connection-multiplier="100" 
  4. connect-timeout="1000" 
  5. max-wait-time="1000" 
  6. auto-connect-retry="true" 
  7. socket-keep-alive="true" 
  8. socket-timeout="10000" 
  9. slave-ok="true" 
  10. write-number="1" 
  11. write-timeout="0" 
  12. write-fsync="true" /> 
  13. </mongo:mongo> 

Get configuration parameters through Java API

 
 
  1. DBCollection logCollection = mongoTemplate.getCollection(collName); 
  2. MongoOptions mongoOptions = logCollection.getDB().getMongo().getMongoOptions(); 
  3. System.out.println(mongoOptions.getSocketTimeout()); 

Last point: ConnectionTimeOut and SocketTimeOut:

A complete request consists of three phases:

  • Establish a connection
  • Data Transmission
  • Disconnect

If the connection time for a server (database) request exceeds ConnectionTimeOut, ConnectionTimeOutException is thrown, that is, the server connection times out and no connection is established within the specified time.

If the connection to the server is successful, data transmission starts.

If the time used by the server to process data exceeds SocketTimeOut, SocketTimeOutExceptin is thrown. That is, the server response times out and the server does not return data to the client within the specified time.

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.