Package com.mongodb

Class MongoClientOptions

java.lang.Object
com.mongodb.MongoClientOptions

@Immutable public class MongoClientOptions extends Object

Various settings to control the behavior of a MongoClient.

Since:
2.10.0
See Also:
  • Method Details

    • builder

      public static MongoClientOptions.Builder builder()
      Creates a builder instance.
      Returns:
      a builder
      Since:
      3.0.0
    • builder

      public static MongoClientOptions.Builder builder(MongoClientOptions options)
      Creates a builder instance.
      Parameters:
      options - existing MongoClientOptions to default the builder settings on.
      Returns:
      a builder
      Since:
      3.0.0
    • builder

      public static MongoClientOptions.Builder builder(MongoClientSettings settings)
      Creates a builder instance from a MongoClientSettings instance.
      Parameters:
      settings - the settings to from which to initialize the builder
      Returns:
      a builder
      Since:
      4.2
    • asMongoClientSettings

      public MongoClientSettings asMongoClientSettings(@Nullable List<ServerAddress> hosts, @Nullable String srvHost, ClusterConnectionMode clusterConnectionMode, @Nullable MongoCredential credential)
      Translate this instance into MongoClientSettings.
      Parameters:
      hosts - the seed list of hosts to connect to, which must be null if srvHost is not
      srvHost - the SRV host name, which must be null if hosts is not
      clusterConnectionMode - the connection mode
      credential - the credential, which may be null
      Returns:
      the settings
      Since:
      4.2
      See Also:
    • getApplicationName

      @Nullable public String getApplicationName()
      Gets the logical name of the application using this MongoClient. The application name may be used by the client to identify the application to the server, for use in server logs, slow query logs, and profile collection.

      Default is null.

      Returns:
      the application name, which may be null
      Since:
      3.4
      Since server release
      3.4
    • getCompressorList

      public List<MongoCompressor> getCompressorList()
      Gets the compressors to use for compressing messages to the server. The driver will use the first compressor in the list that the server is configured to support.

      Default is the empty list.

      Returns:
      the compressors
      Since:
      3.6
      Since server release
      3.4
    • getConnectionsPerHost

      public int getConnectionsPerHost()

      The maximum number of connections allowed per host for this MongoClient instance. Those connections will be kept in a pool when idle. Once the pool is exhausted, any operation requiring a connection will block waiting for an available connection.

      Default is 100.

      Returns:
      the maximum size of the connection pool per host; if 0, then there is no limit.
    • getMinConnectionsPerHost

      public int getMinConnectionsPerHost()

      The minimum number of connections per host for this MongoClient instance. Those connections will be kept in a pool when idle, and the pool will ensure over time that it contains at least this minimum number.

      Default is 0.

      Returns:
      the minimum size of the connection pool per host
    • getServerSelectionTimeout

      public int getServerSelectionTimeout()

      Gets the server selection timeout in milliseconds, which defines how long the driver will wait for server selection to succeed before throwing an exception.

      Default is 30,000 milliseconds. A value of 0 means that it will timeout immediately if no server is available. A negative value means to wait indefinitely.

      Returns:
      the server selection timeout in milliseconds.
    • getMaxWaitTime

      public int getMaxWaitTime()

      The maximum wait time in milliseconds that a thread may wait for a connection to become available.

      Default is 120,000 milliseconds. A value of 0 means that it will not wait. A negative value means to wait indefinitely.

      Returns:
      the maximum wait time.
    • getMaxConnectionIdleTime

      public int getMaxConnectionIdleTime()
      The maximum idle time of a pooled connection. A zero value indicates no limit to the idle time. A pooled connection that has exceeded its idle time will be closed and replaced when necessary by a new connection.

      Default is 0, indicating no limit to the idle time.

      Returns:
      the maximum idle time, in milliseconds
      Since:
      2.12
    • getMaxConnectionLifeTime

      public int getMaxConnectionLifeTime()
      The maximum life time of a pooled connection. A zero value indicates no limit to the life time. A pooled connection that has exceeded its life time will be closed and replaced when necessary by a new connection.

      Default is 0, indicating no limit to the life time.

      Returns:
      the maximum life time, in milliseconds
      Since:
      2.12
    • getMaxConnecting

      public int getMaxConnecting()
      The maximum number of connections a pool may be establishing concurrently. Establishment of a connection is a part of its life cycle starting after a ConnectionCreatedEvent and ending before a ConnectionReadyEvent.

      Default is 2.

      Returns:
      The maximum number of connections a pool may be establishing concurrently.
      Since:
      4.4
      See Also:
    • getMaintenanceInitialDelay

      public long getMaintenanceInitialDelay()
      Returns the period of time to wait before running the first maintenance job on each connection pool.

      Default is 0 ms.

      Returns:
      the time period to wait in milliseconds
      Since:
      4.7
      See Also:
    • getMaintenanceFrequency

      public long getMaintenanceFrequency()
      Returns the time period between runs of the maintenance job on each connection pool.

      Default is 60,000 ms.

      Returns:
      the time period between runs of the maintainance job in milliseconds
      Since:
      4.7
      See Also:
    • getConnectTimeout

      public int getConnectTimeout()

      The connection timeout in milliseconds. A value of 0 means no timeout. It is used solely when establishing a new connection Socket.connect(java.net.SocketAddress, int)

      Default is 10,000 milliseconds.

      Returns:
      the socket connect timeout
    • getSocketTimeout

      public int getSocketTimeout()

      The socket timeout in milliseconds. It is used for I/O socket read operations Socket.setSoTimeout(int)

      Default is 0 and means no timeout.

      Returns:
      the socket timeout, in milliseconds
    • getHeartbeatFrequency

      public int getHeartbeatFrequency()
      Gets the heartbeat frequency. This is the frequency that the driver will attempt to determine the current state of each server in the cluster.

      Default is 10,000 milliseconds.

      Returns:
      the heartbeat frequency, in milliseconds
      Since:
      2.12
    • getMinHeartbeatFrequency

      public int getMinHeartbeatFrequency()
      Gets the minimum heartbeat frequency. In the event that the driver has to frequently re-check a server's availability, it will wait at least this long since the previous check to avoid wasted effort.

      Default is 500 milliseconds.

      Returns:
      the minimum heartbeat frequency, in milliseconds
      Since:
      2.13
    • getHeartbeatConnectTimeout

      public int getHeartbeatConnectTimeout()

      Gets the connect timeout for connections used for the cluster heartbeat.

      Default is 20,000 milliseconds.

      Returns:
      the heartbeat connect timeout, in milliseconds
      Since:
      2.12
    • getHeartbeatSocketTimeout

      public int getHeartbeatSocketTimeout()
      Gets the socket timeout for connections used for the cluster heartbeat.

      Default is 20,000 milliseconds.

      Returns:
      the heartbeat socket timeout, in milliseconds
      Since:
      2.12
    • getLocalThreshold

      public int getLocalThreshold()

      Gets the local threshold. When choosing among multiple MongoDB servers to send a request, the MongoClient will only send that request to a server whose ping time is less than or equal to the server with the fastest ping time plus the local threshold.

      For example, let's say that the client is choosing a server to send a query when the read preference is ReadPreference.secondary(), and that there are three secondaries, server1, server2, and server3, whose ping times are 10, 15, and 16 milliseconds, respectively. With a local threshold of 5 milliseconds, the client will send the query to either server1 or server2 (randomly selecting between the two).

      Default is 15 milliseconds.

      Returns:
      the local threshold, in milliseconds
      Since:
      2.13.0
      MongoDB documentation
      Local Threshold
    • getRequiredReplicaSetName

      @Nullable public String getRequiredReplicaSetName()

      Gets the required replica set name. With this option set, the MongoClient instance will

      1. Connect in replica set mode, and discover all members of the set based on the given servers
      2. Make sure that the set name reported by all members matches the required set name.
      3. Refuse to service any requests if any member of the seed list is not part of a replica set with the required name.
      Returns:
      the required replica set name
      Since:
      2.12
    • isSslEnabled

      public boolean isSslEnabled()
      Whether to use SSL.

      Default is false.

      Returns:
      true if SSL should be used
      Since:
      3.0
    • isSslInvalidHostNameAllowed

      public boolean isSslInvalidHostNameAllowed()
      Returns whether invalid host names should be allowed if SSL is enabled. Take care before setting this to true, as it makes the application susceptible to man-in-the-middle attacks.

      Default is false.

      Returns:
      true if invalid host names are allowed.
    • getSslContext

      @Nullable public SSLContext getSslContext()
      Returns the SSLContext. This property is ignored when either sslEnabled is false or socketFactory is non-null.
      Returns:
      the configured SSLContext, which may be null. In that case SSLContext.getDefault() will be used when SSL is enabled.
      Since:
      3.5
    • getReadPreference

      public ReadPreference getReadPreference()

      The read preference to use for queries, map-reduce, aggregation, and count.

      Default is ReadPreference.primary().

      Returns:
      the read preference
      See Also:
    • getWriteConcern

      public WriteConcern getWriteConcern()

      The write concern to use.

      Default is WriteConcern.ACKNOWLEDGED.

      Returns:
      the write concern
      See Also:
    • getRetryWrites

      public boolean getRetryWrites()
      Returns true if writes should be retried if they fail due to a network error or other retryable error.

      Starting with the 3.11.0 release, the default value is true

      Returns:
      the retryWrites value
      Since:
      3.6
      Since server release
      3.6
    • getRetryReads

      public boolean getRetryReads()
      Returns true if reads should be retried if they fail due to a network error or other retryable error.
      Returns:
      the retryReads value
      Since:
      3.11
      Since server release
      3.6
    • getReadConcern

      public ReadConcern getReadConcern()

      The read concern to use.

      Returns:
      the read concern
      Since:
      3.2
      MongoDB documentation
      Read Concern
      Since server release
      3.2
    • getCodecRegistry

      public CodecRegistry getCodecRegistry()

      The codec registry to use. By default, a MongoClient will be able to encode and decode instances of Document.

      Note that instances of DB and DBCollection do not use the registry, so it's not necessary to include a codec for DBObject in the registry.

      Returns:
      the codec registry
      Since:
      3.0
      See Also:
    • getUuidRepresentation

      public UuidRepresentation getUuidRepresentation()
      Gets the UUID representation to use when encoding instances of UUID and when decoding BSON binary values with subtype of 3.

      The default is UuidRepresentation.UNSPECIFIED, If your application stores UUID values in MongoDB, you must set this value to the desired representation. New applications should prefer UuidRepresentation.STANDARD, while existing Java applications should prefer UuidRepresentation.JAVA_LEGACY. Applications wishing to interoperate with existing Python or .NET applications should prefer UuidRepresentation.PYTHON_LEGACY or UuidRepresentation.C_SHARP_LEGACY, respectively. Applications that do not store UUID values in MongoDB don't need to set this value.

      Returns:
      the UUID representation, which may not be null
      Since:
      3.12
    • getSrvMaxHosts

      @Nullable public Integer getSrvMaxHosts()
      Gets the maximum number of hosts to connect to when using SRV protocol.
      Returns:
      the maximum number of hosts to connect to when using SRV protocol. Defaults to null.
      Since:
      4.5
    • getSrvServiceName

      public String getSrvServiceName()
      Gets the SRV service name.

      The SRV resource record (RFC 2782) service name, which is limited to 15 characters (RFC 6335 section 5.1). If specified, it is combined with the single host name as follows: _srvServiceName._tcp.hostName. The combined string is an SRV resource record name (RFC 1035 section 2.3.1), which is limited to 255 characters (RFC 1035 section 2.3.4).

      Returns:
      the SRV service name, which defaults to "mongodb"
      Since:
      4.5
    • getServerApi

      @Nullable public ServerApi getServerApi()
      Gets the server API to use when sending commands to the server.
      Returns:
      the server API, which may be null
      Since:
      4.3
    • getTimeout

      @Alpha(CLIENT) @Nullable public Long getTimeout()
      The time limit for the full execution of an operation in Milliseconds.

      If set the following deprecated options will be ignored: waitQueueTimeoutMS, socketTimeoutMS, wTimeoutMS, maxTimeMS and maxCommitTimeMS

      • null means that the timeout mechanism for operations will defer to using:
        • waitQueueTimeoutMS: The maximum wait time in milliseconds that a thread may wait for a connection to become available
        • socketTimeoutMS: How long a send or receive on a socket can take before timing out.
        • wTimeoutMS: How long the server will wait for the write concern to be fulfilled before timing out.
        • maxTimeMS: The cumulative time limit for processing operations on a cursor. See: cursor.maxTimeMS.
        • maxCommitTimeMS: The maximum amount of time to allow a single commitTransaction command to execute. See: TransactionOptions.getMaxCommitTime(java.util.concurrent.TimeUnit).
      • 0 means infinite timeout.
      • > 0 The time limit to use for the full execution of an operation.
      Returns:
      the timeout in milliseconds
      Since:
      5.2
    • getServerSelector

      @Nullable public ServerSelector getServerSelector()
      Gets the server selector.

      The server selector augments the normal server selection rules applied by the driver when determining which server to send an operation to. At the point that it's called by the driver, the ClusterDescription which is passed to it contains a list of ServerDescription instances which satisfy either the configured ReadPreference for any read operation or ones that can take writes (e.g. a standalone, mongos, or replica set primary).

      The server selector can then filter the ServerDescription list using whatever criteria that is required by the application.

      After this selector executes, two additional selectors are applied by the driver:

      • select from within the latency window
      • select a random server from those remaining

      To skip the latency window selector, an application can:

      • configure the local threshold to a sufficiently high value so that it doesn't exclude any servers
      • return a list containing a single server from this selector (which will also make the random member selector a no-op)
      Returns:
      the server selector, which may be null
      Since:
      3.6
    • getClusterListeners

      public List<ClusterListener> getClusterListeners()
      Gets the list of added ClusterListener. The default is an empty list.
      Returns:
      the unmodifiable list of cluster listeners
      Since:
      3.3
    • getCommandListeners

      public List<CommandListener> getCommandListeners()
      Gets the list of added CommandListener.

      Default is an empty list.

      Returns:
      the unmodifiable list of command listeners
      Since:
      3.1
    • getConnectionPoolListeners

      public List<ConnectionPoolListener> getConnectionPoolListeners()
      Gets the list of added ConnectionPoolListener. The default is an empty list.
      Returns:
      the unmodifiable list of connection pool listeners
      Since:
      3.5
    • getServerListeners

      public List<ServerListener> getServerListeners()
      Gets the list of added ServerListener. The default is an empty list.
      Returns:
      the unmodifiable list of server listeners
      Since:
      3.3
    • getServerMonitorListeners

      public List<ServerMonitorListener> getServerMonitorListeners()
      Gets the list of added ServerMonitorListener. The default is an empty list.
      Returns:
      the unmodifiable list of server monitor listeners
      Since:
      3.3
    • getDbDecoderFactory

      public DBDecoderFactory getDbDecoderFactory()
      Override the decoder factory.

      Default is for the standard Mongo Java driver configuration.

      Returns:
      the decoder factory
    • getDbEncoderFactory

      public DBEncoderFactory getDbEncoderFactory()
      Override the encoder factory.

      Default is for the standard Mongo Java driver configuration.

      Returns:
      the encoder factory
    • isCursorFinalizerEnabled

      public boolean isCursorFinalizerEnabled()

      Gets whether there is a a finalize method created that cleans up instances of DBCursor that the client does not close. If you are careful to always call the close method of DBCursor, then this can safely be set to false.

      Default is true.

      Returns:
      whether finalizers are enabled on cursors
      See Also:
    • getAutoEncryptionSettings

      @Nullable public AutoEncryptionSettings getAutoEncryptionSettings()
      Gets the auto-encryption settings
      Returns:
      the auto-encryption settings, which may be null
      Since:
      3.11
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object