|
| 1 | +.. _mcs-cluster-settings: |
| 2 | + |
| 3 | +================ |
| 4 | +Cluster Settings |
| 5 | +================ |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +.. facet:: |
| 14 | + :name: genre |
| 15 | + :values: reference |
| 16 | + |
| 17 | +.. meta:: |
| 18 | + :keywords: timeout, replica set |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn about how the {+driver-short+} manages clusters. |
| 24 | + |
| 25 | +You can specify settings for your clusters using either a :ref:`connection |
| 26 | +string <connection-uri>` or by passing a ``MongoClientSettings`` object to the |
| 27 | +:ref:`MongoClients <mongoclient>` contructor. Select the :guilabel:`Connection |
| 28 | +String` or :guilabel:`MongoClientSettings` tab to see the options available: |
| 29 | + |
| 30 | + |
| 31 | +.. tabs:: |
| 32 | + |
| 33 | + .. tab:: Connection String |
| 34 | + :tabid: uri |
| 35 | + |
| 36 | + .. list-table:: |
| 37 | + :header-rows: 1 |
| 38 | + :widths: 20 10 20 |
| 39 | + |
| 40 | + * - Option Name |
| 41 | + - Type |
| 42 | + - Description |
| 43 | + |
| 44 | + * - **serverSelectionTimeoutMS** |
| 45 | + - integer |
| 46 | + - Specifies the maximum amount of time, in milliseconds, the driver |
| 47 | + will wait for server selection to succeed before throwing an |
| 48 | + exception. |
| 49 | + |
| 50 | + | **Default**: ``30000`` (30 seconds) |
| 51 | + |
| 52 | + * - **localThresholdMS** |
| 53 | + - integer |
| 54 | + - When communicating with multiple instances of MongoDB in a replica |
| 55 | + set, the driver will only send requests to a server whose |
| 56 | + response time is less than or equal to the server with the fastest |
| 57 | + response time plus the local threshold, in milliseconds. |
| 58 | + |
| 59 | + | **Default**: ``15`` |
| 60 | + |
| 61 | + * - **heartbeatFrequencyMS** |
| 62 | + - integer |
| 63 | + - Specifies the frequency, in milliseconds that the driver will |
| 64 | + wait between attempts to determine the current state of each |
| 65 | + server in the cluster. |
| 66 | + |
| 67 | + | **Default**: ``10000`` (10 seconds) |
| 68 | + |
| 69 | + * - **replicaSet** |
| 70 | + - string |
| 71 | + - Specifies that the :ref:`connection string <connection-uri>` |
| 72 | + provided includes multiple hosts. When specified, the driver |
| 73 | + attempts to find all members of that set. |
| 74 | + |
| 75 | + | **Default**: ``null`` |
| 76 | + |
| 77 | + * - **directConnection** |
| 78 | + - boolean |
| 79 | + - Specifies that the driver must connect to the host directly. This |
| 80 | + maps to applying ``mode(ClusterConnectionMode.SINGLE)`` to your ``MongoClientSettings``. |
| 81 | + |
| 82 | + | **Default**: ``false`` |
| 83 | + |
| 84 | + * - **srvServiceName** |
| 85 | + - string |
| 86 | + - Specifies the service name of the `SRV resource records <https://www.rfc-editor.org/rfc/rfc2782>`__ the driver retrieves to construct your :manual:`seed list </reference/glossary/#std-term-seed-list>`. You must use the :manual:`DNS Seed List Connection Format </reference/connection-string/#dns-seed-list-connection-format>` in your :ref:`connection URI <connection-uri>` to use this option. |
| 87 | + |
| 88 | + | **Default**: ``mongodb`` |
| 89 | + |
| 90 | + .. tab:: MongoClientSettings |
| 91 | + :tabid: MongoClient |
| 92 | + |
| 93 | + Chain the `applyToClusterSettings() |
| 94 | + <{+api+}/apidocs/mongodb-driver-core/com/mongodb/MongoClientSettings.Builder.html#applyToClusterSettings(com.mongodb.Block)>`__ |
| 95 | + method to modify the driver's behavior when interacting with your MongoDB |
| 96 | + cluster. |
| 97 | + |
| 98 | + The following table describes the methods you can chain to your |
| 99 | + settings to modify the driver's behavior: |
| 100 | + |
| 101 | + .. list-table:: |
| 102 | + :header-rows: 1 |
| 103 | + :stub-columns: 1 |
| 104 | + :widths: 40 60 |
| 105 | + |
| 106 | + * - Method |
| 107 | + - Description |
| 108 | + |
| 109 | + * - ``addClusterListener()`` |
| 110 | + - Adds a listener for cluster-related events. |
| 111 | + |
| 112 | + * - ``applyConnectionString()`` |
| 113 | + - Uses the settings from a ``ConnectionString`` object. |
| 114 | + |
| 115 | + * - ``applySettings()`` |
| 116 | + - Uses the cluster settings specified in a ``ClusterSettings`` object. |
| 117 | + |
| 118 | + * - ``hosts()`` |
| 119 | + - Sets all the specified locations of a Mongo deployment. |
| 120 | + |
| 121 | + * - ``localThreshold()`` |
| 122 | + - | Sets the amount of time that a server’s round trip can take and still be eligible for server selection. |
| 123 | + | |
| 124 | + | **Default**: ``15 milliseconds`` |
| 125 | + |
| 126 | + * - ``mode()`` |
| 127 | + - Sets how to connect to a MongoDB deployment. |
| 128 | + |
| 129 | + * - ``requiredClusterType()`` |
| 130 | + - Sets the type of cluster required for the cluster. |
| 131 | + |
| 132 | + * - ``requiredReplicaSetName()`` |
| 133 | + - Sets the replica set name required for the cluster. |
| 134 | + |
| 135 | + * - ``serverSelectionTimeout()`` |
| 136 | + - | Sets the maximum time to select a primary node before throwing a timeout exception. |
| 137 | + | |
| 138 | + | **Default**: ``30 seconds`` |
| 139 | + |
| 140 | + * - ``serverSelector()`` |
| 141 | + - Adds a server selector to apply before server selection. |
| 142 | + |
| 143 | + * - ``srvHost()`` |
| 144 | + - | Sets the host name to use to look up an SRV DNS record to find the MongoDB hosts. |
| 145 | + | |
| 146 | + | If you want to enable the processing of TXT records associated with the host, specify the SRV host in the connection string using the ``applyConnectionString()`` method. |
| 147 | + | |
| 148 | + | For example: |
| 149 | + |
| 150 | + .. code-block:: java |
| 151 | + :emphasize-lines: 3 |
| 152 | + |
| 153 | + MongoClient mongoClient = |
| 154 | + MongoClients.create(MongoClientSettings.builder() |
| 155 | + .applyConnectionString(new ConnectionString("mongodb+srv://host1.acme.com"))) |
| 156 | + |
| 157 | + * - ``srvMaxHosts()`` |
| 158 | + - | Sets the maximum number of hosts the driver can connect to when using the DNS seedlist (SRV) connection protocol, identified by the ``mongodb+srv`` connection string prefix. |
| 159 | + | |
| 160 | + | Throws an exception if you are not using the SRV connection protocol. |
| 161 | + |
| 162 | + Example |
| 163 | + ~~~~~~~ |
| 164 | + |
| 165 | + This example specifies for the driver to connect directly to a server, |
| 166 | + regardless of the type of MongoDB cluster it's a part of: |
| 167 | + |
| 168 | + .. literalinclude:: /includes/fundamentals/code-snippets/MCSettings.java |
| 169 | + :start-after: begin ClusterSettings |
| 170 | + :end-before: end ClusterSettings |
| 171 | + :language: java |
| 172 | + :emphasize-lines: 3-4 |
| 173 | + :dedent: |
| 174 | + |
| 175 | + .. tip:: |
| 176 | + |
| 177 | + This is analogous to the ``directConnection`` parameter you can specify |
| 178 | + in your connection URI. See :ref:`<connection-options>` for more |
| 179 | + information. |
0 commit comments