Vipin
Vipin

Reputation: 811

how to set maximum time for spring data mongo query

I want to kill slow queries which run for more than 2 hours.

can i use socket-timeout in MongoOptions class for killing slow queries in spring data mongo db?

Please help.

Upvotes: 1

Views: 3597

Answers (1)

Stennie
Stennie

Reputation: 65303

Socket timeouts will close the connection from the client point of view, but the server will continue to process the query if there is still work in progress (for example, fetching a batch of data).

If you are using a version of MongoDB server prior to 2.6, you can kill long running operations via the db.killOp() command .. however you have to ensure that you are killing the expected client operations and not internal server threads. It can be helpful to add a unique $comment to your long running queries so they can be identified via db.currentOp(true).

MongoDB 2.6+ added a new maxTimeMS cursor option to ensure the server kills the cursor after a certain amount of cumulative processing time (in milliseconds). You'll need to be using the 2.12 (or newer) version of the MongoDB Java driver (which can be set in your pom.xml with <mongodb.driver.version>).

For more background on maxTimeMS and socket timeouts see: maxTimeMS() and Query Optimizer Introspection in MongoDB 2.6.

Upvotes: 2

Related Questions