@@ -101,6 +101,70 @@ for the correct environment.
101
101
The aforementioned ``detect-extension`` script can also be used to determine the
102
102
appropriate DLL for your PHP environment.
103
103
104
+ Connection Handling and Persistence
105
+ -----------------------------------
106
+
107
+ Connections to the MongoDB deployment are handled by the ``libmongoc``
108
+ library and the :php:`{+extension-short+} <mongodb>`. When you construct
109
+ a :phpclass:`MongoDB\Client` instance, the {+library-short+} creates a
110
+ :php:`MongoDB\Driver\Manager <class.mongodb-driver-manager>` instance by using the
111
+ same connection string and options. The extension also uses those constructor
112
+ arguments to derive a hash key for persistent ``libmongoc`` clients. If
113
+ you previously persisted a ``libmongoc`` client by using a key, it is
114
+ reused. Otherwise, a new ``libmongoc`` client is created and persisted
115
+ for the lifetime of the PHP worker process. You can learn more about
116
+ this process in the :php:`{+extension-short+} documentation
117
+ <manual/en/mongodb.connection-handling.php>`.
118
+
119
+ Each ``libmongoc`` client maintains its own connections to the MongoDB deployment
120
+ and a view of its topology. When you reuse a persistent ``libmongoc`` client, the
121
+ {+library-short+} can avoid the overhead of establishing new connections and
122
+ rediscovering the topology. This approach generally improves performance and is
123
+ the driver's default behavior.
124
+
125
+ Persistent ``libmongoc`` clients are not freed until the PHP worker process
126
+ ends. This means that connections to a MongoDB deployment might remain open
127
+ after a ``MongoDB\Driver\Manager`` object goes out of scope. While this is
128
+ typically not an issue for applications that connect to one MongoDB deployment,
129
+ it might be problematic in some situations, which are described in the
130
+ following list:
131
+
132
+ - PHP-FPM is configured with ``pm.max_requests=0``, so that workers never respawn, and a
133
+ PHP application is deployed many times with small changes to its MongoDB
134
+ connection string or options. This could lead to an accumulation of ``libmongoc``
135
+ client objects in each worker process.
136
+
137
+ - An application occasionally connects to a separate MongoDB deployment in a
138
+ backend component where request latency is not the most important aspect.
139
+
140
+ In the first case, restarting PHP-FPM as part of the application deployment
141
+ allows the application to release any unused ``libmongoc`` clients and still use
142
+ a persistent client for the latest connection string.
143
+
144
+ The second case requires a different solution. Specifying ``true`` for the
145
+ ``disableClientPersistence`` driver option instructs the {+library-short+} to
146
+ create a new ``libmongoc`` client and ensure it is freed when the corresponding
147
+ ``MongoDB\Driver\Manager`` goes out of scope.
148
+
149
+ The following code demonstrates how to set the
150
+ ``disableClientPersistence`` option to ``true`` when creating a client:
151
+
152
+ .. code-block:: php
153
+ :emphasize-lines: 6
154
+
155
+ <?php
156
+
157
+ $client = new MongoDB\Client(
158
+ uri: getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/',
159
+ uriOptions: [],
160
+ driverOptions: ['disableClientPersistence' => true],
161
+ );
162
+
163
+ Use the ``disableClientPersistence`` driver option after careful
164
+ consideration, because opting out of client persistence requires more
165
+ time to establish connections to the MongoDB deployment and discover its
166
+ topology.
167
+
104
168
Server Selection Failures
105
169
-------------------------
106
170
0 commit comments