socket-settings

For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

<!-- Tab options on this page. Append to the .md URL to filter: ?tabs=<id,...> select specific tabs (e.g. ?tabs=nodejs,shell) ?allTabs=true include every tab (no param) default: one tab per tabset

Available tabs: other tabs: uri, MongoClient -->

Socket Settings

Overview

In this guide, you can learn about how the Java driver manages socket settings.

You can specify settings for your sockets by using either a [connection string](https://www.mongodb.com/docs/drivers/java/sync/current/connection/mongoclient.md#std-label-connection-uri or by passing a MongoClientSettings object to the [MongoClients](https://www.mongodb.com/docs/drivers/java/sync/current/connection/mongoclient.md#std-label-mongoclient constructor. Select the Connection String or MongoClientSettings tab to see the options available:

Connection String

Option Name Type Description
connectTimeoutMS integer Specifies the maximum amount of time, in milliseconds, the Java driver waits for a connection to open before timing out. A value of 0 instructs the driver to never time out while waiting for a connection to open. Default: 10000 (10 seconds)
socketTimeoutMS integer Specifies the maximum amount of time, in milliseconds, the Java driver will wait to send or receive a request before timing out. A value of 0 instructs the driver to never time out while waiting to send or receive a request. Default: 0

This example specifies that the driver will time out after 15 seconds of waiting for a connection to open:

ConnectionString connectionString = "mongodb://<host>:<port>/?connectTimeoutMS=15000"
MongoClient mongoClient = MongoClients.create(connectionString)

For more information about these parameters, see the ConnectionString API documentation.