In SQLAlchemy documentation there is an example of handling session with 
context manager:

from contextlib import contextmanager
@contextmanagerdef 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? Some objects like query 
will keep session reference even after session.close(), so created session 
is not guaranteed to end with contextmanager call. Then it may lead to 
'sqlalchemy.exc.InvalidRequestError: is already attached to session' 
exceptions. 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...
Is there any way to list all sqlalchemy session objects? It may be useful 
id debugging.
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.

Reply via email to