On Wed, Jan 31, 2018 at 8:37 AM, Jacek Blocki <blocki1...@gmail.com> wrote: > In SQLAlchemy documentation there is an example of handling session with > context manager: > > from contextlib import contextmanager > > @contextmanager > def session_scope(): > """Provide a transactional scope around a series of operations.""" > session = Session() > try: > yield session > session.commit() > except: > session.rollback() > raise > finally: > session.close() > > > What are real benefits of creating new session vs. returning the same > session kept at module level in above approach?
well you wouldn't want to re-use a module-level session except in a small console application, because such a session isn't threadsafe and "module level" usually implies "global variable". > Some objects like query will > keep session reference even after session.close(), so created session is not > guaranteed to end with contextmanager call. this is true, session.close() does not end the life of the session, you can keep reusing it. close() just empties it out of all objects and transactional state. this does not imply any failure mode. > Then it may lead to > 'sqlalchemy.exc.InvalidRequestError: is already attached to session' > exceptions. not true, because when you call close(), all objects are de-associated from that session. > Of course in the docs there is a warning above approach is for > "advanced developers", who can sort out any issue or risk their advanced > badge revoked... oh. the paragraph about the context manager uses the phrase "the advanced developer", that seems to be misleading. That's not the usual "Warning: this method is for advanced developers" type of warning. it says that with the intent of "don't worry if you don't understand what @contextmanager is yet". I'll modify that language now as it has failed. That will be up on the site within the hour. > Is there any way to list all sqlalchemy session objects? It may be useful id > debugging. ah, well there is though it's not public, but feel free to use it, if you think it's useful it can be provided as a public method: from sqlalchemy.orm import session print(session._sessions.values()) session._sessions is a WeakValueDictionary. > Regards, Jacek > > > -- > SQLAlchemy - > The Python SQL Toolkit and Object Relational Mapper > > http://www.sqlalchemy.org/ > > To post example code, please provide an MCVE: Minimal, Complete, and > Verifiable Example. See http://stackoverflow.com/help/mcve for a full > description. > --- > You received this message because you are subscribed to the Google Groups > "sqlalchemy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to sqlalchemy+unsubscr...@googlegroups.com. > To post to this group, send email to sqlalchemy@googlegroups.com. > Visit this group at https://groups.google.com/group/sqlalchemy. > For more options, visit https://groups.google.com/d/optout. -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.