Connection handling and persistence
Connection and topology persistence (PHP version since 1.2.0)All versions of the extension since 1.2.0 persist the » libmongoc client object in the PHP worker process, which allows it to re-use database connections, authentication states, and topology information across multiple requests.
When MongoDB\Driver\Manager::__construct is
invoked, a hash is created from its arguments (i.e. URI string and array
options). The extension will attempt to find a previously persisted
» libmongoc client object for
that hash. If an existing client cannot be found for the hash, a new client
will be created and persisted for future use. This behavior can be disabled
via the Each client contains its own database connections and a view of the server topology (e.g. standalone, replica set, shard cluster). By persisting the client between PHP requests, the extension is able to re-use established database connections and remove the need for » discovering the server topology on each request. Consider the following example:
The first two Manager objects will share the same
» libmongoc client since
their constructor arguments are identical. The third and fourth objects will
each use their own client. In total, three clients will be created and the
PHP worker executing this script will open two connections to
If the same worker executes the script again in a second request, the three
clients will be re-used and no new connections will be made. Depending on
how long ago the previous request was served, the extension may need to
issue additional Socket persistence (PHP versions before 1.2.0)Versions of the extension before 1.2.0 utilize PHP's Streams API for database connections, using an API within » libmongoc to designate custom handlers for socket communication; however, a new libmongoc client is created for each MongoDB\Driver\Manager. As a result, the extension persists individual database connections but not authentication state or topology information. This means that the extension needs to issue commands at the start of each request to authenticate and » discover the server topology. Database connections are persisted by a hash derived from the server's host, port, and the URI string used to construct the MongoDB\Driver\Manager. The constructor's array options are not included in this hash.
Despite its shortcomings with persisting SSL connections when and topology information, this version of the extension supports all SSL context options since it uses PHP's Streams API. |