The error "SocketTimeoutException" message returned when the MongoDB aggregate query times out.

Source: Internet
Author: User
Tags mongodb mongodb aggregate socket

Aggregate a collection of more than one million records

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

Occasionally, a Read timed out exception occurs.

Com. mongodb. MongoException $ Network: Read operation to server/192.168.10.202: 27017 failed on database adLogTable
At com. mongodb. DBTCPConnector. innerCall (dbtcpconne. 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 (Fig. 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:

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.

Socket-timeout = "10000" // 10 s

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.

<Mongo: mongo host = "$ {mongodb. ip}" id = "mongo202" port = "$ {mongodb. port}">
& Lt; mongo: options connections-per-host = "200"
Threads-allowed-to-block-for-connection-Multi-plier = "100"
Connect-timeout = "1000"
Max-wait-time = "1000"
Auto-connect-retry = "true"
Socket-keep-alive = "true"
Socket-timeout = "10000"
Slave-OK = "true"
Write-number = "1"
Write-timeout = "0"
Write-fsync = "true"/>
</Mongo: mongo>

Get configuration parameters through Java API

DBCollection logCollection = custom template. getCollection (collName );
Optional options exist options = logCollection. getDB (). getMongo (). Getmetadata options ();
System. out. println (gradient options. getSocketTimeout ());

Next: ConnectionTimeOut and SocketTimeOut:

A complete request consists of three phases: 1. Establish a connection 2. Data transmission 3. 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.