Package com.mongodb

Class MongoClientOptions


  • @Immutable
    public class MongoClientOptions
    extends Object

    Various settings to control the behavior of a MongoClient.

    Since:
    2.10.0
    See Also:
    MongoClient
    • Method Detail

      • 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
      • 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
      • 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
      • 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 and write 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:
        ReadPreference.primary()
      • 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:
        MongoClient.getDatabase(java.lang.String)
      • 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
      • 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:
        DBCursor, DBCursor.close()
      • getAutoEncryptionSettings

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

        public int hashCode()
        Overrides:
        hashCode in class Object