PHPORM-256: Disable mongoc_client reuse between connections #3197
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PHPORM-256, fixes #3183
The issue described in the query can happen when multiple connections use the same client config. In that case,
MongoDB\Driver\Manager
reuses the underlyingmongoc_client
instance for performance reasons. However, as the query logger relies on APM callbacks which are tied to themongoc_client
, there is no way for us to keep this separate.Disabling client persistence would solve this problem, but it would potentially introduce performance issues, as the connection would no longer be reused across multiple requests, introducing a delay for connection establishment in every request. However, by using the name of the connection from the config, we can introduce a unique identifier in the driver options, leading to the driver opening separate
mongoc_client
connections for each Laravel database connection, while at the same time leveraging connection persistence. Since the driver does not yield warnings or errors when encountering unknown driver options, we can safely inject this option that ends up being unused. @jmikola let me know if you can think of a better way to fix this.The test in this PR fails without the patch, seeing a logged query in
$secondConnection
before actually running a query through the collection.