Overview
This section describes the MongoDB connection and authentication options you can pass in as parameters of the connection URI in the Java Reactive Streams driver.
Using the Connection URI
If you pass a connection URI to the MongoClient constructor, you can include connection options in the string as <name>=<value> pairs. In the following example, the connection URI contains the connectTimeoutMS option with a value of 60000 and the tls option with a value of true:
public class MongoConnection { public static void main(String[] args) { String uri = "mongodb://<hostname>:<port>/?connectTimeoutMS=60000&tls=true"; try (MongoClient client = MongoClients.create(uri)) { // Use client here } } }
Connection Options
The following sections describe the connection options available in the Java Reactive Streams driver. You can pass these options in as parameters of the connection URI to specify the behavior of the client.
Authentication
Connection Option | Description |
|---|---|
authMechanism | Sets the mechanism the Java Reactive Streams driver uses to authenticate the |
authMechanismProperties | Sets options specific to the authentication mechanism. |
authSource | Sets the source of authentication credentials. The source is typically a |
username | Sets the username for authentication. When this option is included in a connection |
password | Sets the password for authentication. When this option is included in a connection |
Connection Configuration
Connection Option | Description |
|---|---|
connectTimeoutMS | Sets the number of milliseconds the connection can take before timing out. If an |
maxIdleTimeMS | Sets the maximum number of milliseconds that a connection can |
maxLifeTimeMS | Sets the maximum lifetime in milliseconds of a pooled connection. If an |
socketTimeoutMS (deprecated) | This option is deprecated. You can configure this timeout by |
ssl | Sets whether to connect using TLS/SSL. |
sslInvalidHostNameAllowed | Allows invalid host names for TLS/SSL connections. |
tls | Sets whether to connect using TLS. Supersedes the |
tlsAllowInvalidHostnames | Allows invalid host names for TLS connections. Supersedes the |
tlsInsecure | Enables insecure TLS connections when connecting using TLS. |
Connection Pool Configuration
Connection Option | Description |
|---|---|
maxConnecting | Sets the maximum number of connections a pool can establish concurrently. |
maxPoolSize | Sets the maximum number of connections in the connection pool.
If an operation needs a new connection while the connection pool has
|
minPoolSize | Sets the minimum number of connections in the connection pool. |
waitQueueTimeoutMS (deprecated) | This option is deprecated. You can configure this timeout by |
Compressor Configuration
Connection Option | Description |
|---|---|
compressors | Sets preferred compression types, in order, for wire-protocol messages sent to |
zlibCompressionLevel | Sets the compression level for zlib to use. This option accepts |
General Configuration
Connection Option | Description |
|---|---|
directConnection | Sets whether the driver connects directly to the host. |
loadBalanced | Sets whether or not the driver is connecting to MongoDB using a load |
retryWrites | Sets whether or not the driver retries supported write operations if |
retryReads | Sets whether or not the driver retries supported read operations if |
maxAdaptiveRetries | Sets the maximum number of times the driver retries a command |
enableOverloadRetargeting | Sets whether the driver may deprioritize servers during server |
uuidRepresentation | Sets the mechanism the Java Reactive Streams driver uses to encode instances of |
Note
Load Shedding in Intelligent Workload Management (IWM)
If errors with SystemOverloadedError or RetryableError labels are causing failures in your application or appearing in your application logs, Load Shedding may be active on your cluster and your driver may not be upgraded to a version that supports it. We recommend upgrading your MongoDB Java Reactive Streams Driver to version 5.8 or later. If you continue to see these errors after upgrading, you can address them through server-side changes, such as reviewing your Load Shedding configuration, or through application-side changes, such as implementing custom error handling or client-side request throttling. Depending on your situation, application-side changes may be preferable or necessary.
Load Shedding is a feature of IWM, and helps manage server load by rejecting excess operations during sustained overload. This feature is available on MongoDB 9.0 and later.
For more information about IWM or handling overload errors, see the Intelligent Workload Management page or Overload Errors page in the Atlas documentation.
Proxy Configuration
Connection Option | Description |
|---|---|
proxyHost | Establishes the SOCKS5 proxy host to establish a connection through. |
proxyPort | Sets the port number for the SOCKS5 proxy server. Must be a non-negative integer. |
proxyUsername | Sets the username for authenticating with the proxy server. Required if |
proxyPassword | Sets the password for authenticating with the proxy server. Required if |
Read Preference Configuration
Connection Option | Description |
|---|---|
readPreference | Sets the read preference for this connection. Enumerated values are: |
readPreferenceTags | Specifies a tag set as a comma-separated list of |
maxStalenessSeconds | Sets the maximum staleness in seconds. To learn more about staleness, |
Replica Set Configuration
Connection Option | Description |
|---|---|
replicaSet | Specifies the name of the replica set to connect to. |
Server Configuration
Connection Option | Description |
|---|---|
appName | Sets the logical name of the application. |
heartbeatFrequencyMS | The frequency in milliseconds of the driver determining the current state of each server in the cluster. |
localThresholdMS | Sets a ping time threshold for choosing among multiple MongoDB servers |
serverMonitoringMode | Sets the server monitoring mode, which defines the monitoring
protocol to use. The enumerated values are: |
serverSelectionTimeoutMS | Sets how long the driver will wait for server selection to succeed before throwing an exception. |
SRV Configuration
Connection Option | Description |
|---|---|
srvServiceName | Sets the SRV service name. See |
srvMaxHosts | Sets the maximum number of hosts from the SRV record to connect to. |
srvAllowedHostsSuffix | Sets the domain suffix that the driver uses to validate the host names
returned by the SRV record lookup, instead of the domain inferred from
the SRV host name. If the value doesn't begin with a period (
|
Warning
Changing the default SRV host name validation can create security vulnerabilities, because it relaxes a built-in safeguard against DNS spoofing. Specify the narrowest suffix that covers the hosts in your deployment. For example, if your SRV host name is cluster.test.internal.example.com, set srvAllowedHostsSuffix to .internal.example.com. Do not set it to .example.com, which accepts any host in the example.com domain.
Write Concern Configuration
Connection Option | Description |
|---|---|
safe | Sets whether the driver ensures that all writes are acknowledged by the |
journal | Sets whether the driver waits for the server to group commit to the |
w | If set, the driver adds |
wtimeoutMS (deprecated) | This option is deprecated. You can configure this timeout by |
For more information about the connection options in this section, see the Databases and Collections guide.