-
Notifications
You must be signed in to change notification settings - Fork 73
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
refactor: SQLConnector connection lifecycle handling #1393
Comments
There is a mostly-not-breaking way to do this, and I'm going to push a draft PR of that soon. It does...sorta break things, but only for subclasses. The breaking change is as follows: we currently allow someone to pass |
Thanks for the issue / PR! I'll have the engineering team take a look at the PR. |
If you remove |
@BuzzCutNorman you should be able to do that exact thing, by overriding the _engine method instead. E.g. def _engine(self) -> Engine:
"""Return the engine object.
Returns:
The SQLAlchemy Engine that's attached to this SQLConnector instance.
"""
if not self._cached_engine:
eng_prefix = "ep."
eng_config = {f"{eng_prefix}url":self.sqlalchemy_url,f"{eng_prefix}echo":"False"}
if self.config.get('sqlalchemy_eng_params'):
for key, value in self.config['sqlalchemy_eng_params'].items():
eng_config.update({f"{eng_prefix}{key}": value})
self._cached_engine = sqlalchemy.engine_from_config(eng_config, prefix=eng_prefix)
return cast(Engine, self._cached_engine) I just copied/pasted/edited that in the github comment text box so the spacing might be off lol. Hope that makes sense though. Basically, instead of create_sqlalchemy_engine, we're just creating and caching a single engine in _engine. So you'd replace the code you linked to with my snippet above. And you just gotta make sure to hold onto the (new!) Here's the method in the PR that I'm talking about ("_engine") |
Thanks, for the explanation and example. much appreciated. |
Solved by #1394 |
Feature scope
Other
Description
This is a sub-issue broken out of #1352.
This comment explains this sub-issue in detail.
The idea here is to make the SQLConnector correctly handle it's database connection, and take on sole ownership of that job. In other words:
Some more detailed next steps here are:
_connect
that can be used as a context manager, and use that every single time a connection is opened. (An alternative is just to make sure that any connection opened gets closed as soon as it's done, but I think that's not as nice & is more prone to error.)To fully realize this goal, we need to make breaking changes. In particular:
_connection
arg on instantiation_connection
altogetherconnection
andcreate_sqlalchemy_connection
. It's a public method that returns a connection. That encourages bad behavior.create_sqlalchemy_engine
. There shouldn't be a public method for creating or getting a sqlalchemy engine; also there should just be one engine per SQLConnector.The text was updated successfully, but these errors were encountered: