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
The fact SessionMaker.close_all closes all sessions even when called on an instance is confusing #4412
Comments
I would welcome a contribution that makes this a standalone function and applies the appropriate deprecation decorators to the existing method. |
deprecation decorators: https://github.com/sqlalchemy/sqlalchemy/blob/master/lib/sqlalchemy/util/deprecations.py#L25 new close_all function would be in the same level as: https://github.com/sqlalchemy/sqlalchemy/blob/master/lib/sqlalchemy/orm/session.py#L3150 |
@zzzeek thanks for your answer.
Same level as - for instance - It may be useful to also add an instance method on SessionMaker that would close only the sessions of this SessionMaker? The deprecated message on the class method would then mention both methods. What do you think? |
yes, e.g. not a method, a module level function
that would be quite complicated to add and I don't think there's really much of a use case for "close_all" overall, plus the long term future of "sessionmaker" itself is somewhat doubtful.
"close all" IMO is a so-called "utility" use case, that is, not a normal thing you should be doing, it's more like when you are writing teardown schemes in test suites where you need to do blunt things so I think just a module-level function is sufficient. if OTOH you really have a real use case for sessionmaker.close_all() I'd love to hear it. |
When calling SessionMaker.close_all, it closes all the opened sessions by sqlalchemy, which can be error-prone and confusing, if called from a SessionMaker instance. This commit makes this function standalone, as it is not conceptually linked to a sessionmaker anyway, and deprecates the old one. Fixes sqlalchemy#4412
@zzzeek so I have a branch ready, but I'm not sure what test I can add to it (the PR template makes it quite clear I need one!). Do you have a suggestion? |
ha, that PR template is inspired more by the unsolicited "drive by" PRs I keep getting, this PR is at least agreed upon and sanctioned. I took a look to see if we have any close_all tests:
based on the names of those tests I can say that's fixture teardown code, not an actual test of close_all() itself. So general session-ish tests go into test/orm/test_session.py, and util-ish things like close_all can be in SessionUtilTest: https://github.com/sqlalchemy/sqlalchemy/blob/master/test/orm/test_session.py#L148 . session.close() is also weird in that the session doesn't go into a "closed" state, so the name is not even that great, it should be something like .reset() someday, but not today. here's a test that works there, which would be modified to use the new function:
also for the deprecation warning, there's a decorator you should use which you can see used here: https://github.com/sqlalchemy/sqlalchemy/blob/master/lib/sqlalchemy/orm/session.py#L1758 when you put that decorator on the close_all() classmethod, the other tests that are calling it in their teardown are going to break, because we fail if a warning occurs. so the other files that are using Session.close_all() normally need to be modified to use the new function. Here's how to run the tests in a fairly quick way (most PRs I get, even when they have tests, the tests break, meaning they never ran the tests):
|
Thanks for your input, and the quickstart tips!
Yeah, no, not really actually. Let's forget it. |
I mean, the way pywps uses it here does seem very strange to me. Unless I missed something, it should probably not be called at all. |
When calling SessionMaker.close_all, it closes all the opened sessions by sqlalchemy, which can be error-prone and confusing, if called from a SessionMaker instance. This commit makes this function standalone, as it is not conceptually linked to a sessionmaker anyway, and deprecates the old one. Fixes sqlalchemy#4412
Augustin Trancart has proposed a fix for this issue in the master branch: Add standalone orm.close_all method and deprecate SessionMaker.close_all https://gerrit.sqlalchemy.org/1090 |
When calling SessionMaker.close_all, it closes all the opened sessions by sqlalchemy, which can be error-prone and confusing, if called from a SessionMaker instance. This commit makes this function standalone, as it is not conceptually linked to a sessionmaker anyway, and deprecates the old one. Fixes: sqlalchemy#4412
When calling SessionMaker.close_all, it closes all the opened sessions by sqlalchemy, which can be error-prone and confusing, if called from a SessionMaker instance. This commit makes this function standalone, as it is not conceptually linked to a sessionmaker anyway, and deprecates the old one. Fixes: sqlalchemy#4412
Augustin Trancart has proposed a fix for this issue in the master branch: Add standalone orm.close_all method and deprecate SessionMaker.close_all https://gerrit.sqlalchemy.org/1090 |
Description
When calling SessionMaker.close_all, it closes all the opened sessions by sqlalchemy, which can be error-prone and confusing.
use case
I'm developing a project using a lib called pywps. Pywps has its own SessionMaker instance (in memory db) and my apps has a postgresql db with its own SessionMaker too.
When pywps calls SessionMaker.close_all() here, all my sessions are also closed at the same time.
While pywps should probably not do that (
close_all
is a classmethod after all, and the documentation does say "close all session in memory"), I still find this fact confusing for users. Intuitively I would have expected this call to close only the Session associated with this SessionMaker.Suggestion
Maybe
close_all
on a session_maker instance should only close sessions made by this instance ?Thanks!
The text was updated successfully, but these errors were encountered: