On Thu, Nov 15, 2018 at 11:16 AM Chris Withers <[email protected]> wrote:
>
> 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 guess that documentation should be updated, that yes that workflow
is available, it's documented in terms of using "begin()" with
autocommit mode:

https://docs.sqlalchemy.org/en/latest/orm/session_transaction.html#session-autocommit

I see your example also uses begin(), but that's not what we have at
https://docs.sqlalchemy.org/en/latest/orm/session_transaction.html#managing-transactions,
that's a Session without the begin() as it's not in autocommit.
Which I don't want people to use :)

So....at the moment you can still say this:

    with session.transaction:

which will do the commit and rollback but not the close().


there's kind of too many options with the Session right now to make
this one size fits all unless you have suggestions.



>
> 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 [email protected].
> To post to this group, send email to [email protected].
> 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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to