Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHPORM-256: Disable mongoc_client reuse between connections #3197

Merged
merged 1 commit into from
Nov 11, 2024

Conversation

alcaeus
Copy link
Member

@alcaeus alcaeus commented Nov 7, 2024

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 underlying mongoc_client instance for performance reasons. However, as the query logger relies on APM callbacks which are tied to the mongoc_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.

@alcaeus alcaeus requested a review from jmikola November 7, 2024 12:04
@alcaeus alcaeus requested a review from a team as a code owner November 7, 2024 12:04
Copy link
Member

@jmikola jmikola left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This guarantees correctness for query logging, although I expect it paves the way for some future user inquiry about connection usage. Let's keep that in mind and revisit at a later point to decide if we should add something to the Laravel docs about how the underlying driver handles client persistence.

@GromNaN
Copy link
Member

GromNaN commented Nov 8, 2024

we should add something to the Laravel docs about how the underlying driver handles client persistence.

Is there such documentation for the MongoDB extension or library?

@jmikola
Copy link
Member

jmikola commented Nov 8, 2024

Is there such documentation for the MongoDB extension or library?

Connection Handling and Persistence covers this in the PHPC docs.

PHPLIB-1278 would add a FAQ entry about using the disableClientPersistence option, but this otherwise isn't something discussed at all within the PHPLIB docs.

@alcaeus alcaeus merged commit bbff3cb into mongodb:5.1 Nov 11, 2024
27 checks passed
@alcaeus alcaeus deleted the phporm-256-duplicate-query-logging branch November 11, 2024 11:29
@GromNaN GromNaN added the bug label Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants