-
Notifications
You must be signed in to change notification settings - Fork 104
feat: add database param to connect() for reusing session pool #1493
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
base: main
Are you sure you want to change the base?
feat: add database param to connect() for reusing session pool #1493
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Summary of ChangesHello @waiho-gumloop, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a significant improvement to how database connections are managed by allowing multiple DBAPI connections to share a single Spanner session pool. By enabling the Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces a useful feature for reusing session pools by passing a Database object to connect(). However, the current implementation can lead to confusion because it silently ignores several other connection parameters when a database object is provided. I've added a specific comment with a suggestion to include validation checks to prevent potential misconfigurations. Additionally, this new functionality is not covered by tests. Please add unit tests to verify the new behavior, including the proposed validation logic.
| if database is not None: | ||
| instance = database._instance | ||
| conn = Connection(instance, database, **kwargs) | ||
| conn._own_pool = False # Don't destroy the shared pool on close | ||
| return conn |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a database object is provided, several other parameters to connect() are ignored because the connection is created directly from the database object. This can be confusing and lead to subtle bugs if a user provides these parameters expecting them to have an effect.
Since instance_id is a required argument, it's particularly problematic as it must be provided but is then not used to establish the connection (other than for a potential mismatch).
To improve the API's robustness and prevent misconfiguration, I recommend adding validation to ensure that when database is provided:
- The required
instance_idmatches the one in thedatabaseobject. - Other conflicting and redundant parameters are not provided.
if database is not None:
if instance_id != database._instance.instance_id:
raise ValueError(
f"The provided `instance_id` ('{instance_id}') does not match the "
f"instance ID of the `database` object ('{database._instance.instance_id}')."
)
# These parameters are used to create a new client/database, so they conflict
# with providing a pre-existing database object.
conflicting_params = {
"database_id": database_id,
"project": project,
"credentials": credentials,
"pool": pool,
"client": client,
"database_role": database_role,
"experimental_host": experimental_host,
}
provided_conflicting = [
name for name, value in conflicting_params.items() if value is not None
]
if provided_conflicting:
raise ValueError(
"When a `database` object is provided, the following parameters "
f"are redundant and must not be set: {', '.join(provided_conflicting)}"
)
instance = database._instance
conn = Connection(instance, database, **kwargs)
conn._own_pool = False # Don't destroy the shared pool on close
return conn
Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:
Fixes #<issue_number_goes_here> 🦕