Hi All,

I'm sure I've asked this before, but a google through the archives couldn't find it.

Does SQLAlchemy provide a context manager that handles the session lifecycle described here?
https://docs.sqlalchemy.org/en/latest/orm/session_transaction.html#managing-transactions

I mean, it should be as simple as the following, right?

@contextmanager
def transaction(session):
    session.begin()
    try:
        yield
        session.commit()
    except:
        session.rollback()
        raise
    finally:
        session.close()

...I'd just prefer not to copy and paste that across libraries and projects ;-)

I remember Mike having good reasons against this, but I can't with them, so what would be bad about making sessions work like this:

with session:
   ...

...and have that do the above?

cheers,

Chris

--
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