On 18/11/2018 23:38, Mike Bayer wrote:
On Sun, Nov 18, 2018, 6:22 PM Chris Withers <[email protected]
<mailto:[email protected]> wrote:
>> 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 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
That chunk is a little confusing, I wish there was a (default) mode
that
said "start a transaction when I ask for one, don't have one open
otherwise" - "idle in transaction" does make Postgres DBAs sad...
This is how all dbapi drivers work. There's no begin method. The
Session won't leave you idle in transaction because it doesn't start
working on a connection (and hence start a transaction) until you start
working with it.
Ah, okay.
So....what happens if you run SQL on your Session if you haven't called
"begin()"? Raise an error ?
That would be fine :-)
I would say, when you make your
Session(), thats the start of your work in a defer to thread
Yep, using the context manager I snipped out above is working fine.
(although I mistakenly tried to have the context manager wrapped
*around* the deferToThread, and yeah, that turns out to end pretty badly
;-) - not had Python segfault on my in a while!)
> So....at the moment you can still say this:
>
> with session.transaction:
>
> which will do the commit and rollback but not the close().
Potentially silly question: when is it useful to commit the session but
not close it?
Still curious about this, gues I could RTSL or find the bit of the
manual, but I'd prefer to not misinterpret either!
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.