get the connectable first:
connection = engine_or_connection.connect()
then do your context manager from that.
the connection returned, if engine_or_connection is already a
Connection, is "branched", meaning it is safe to call close() on it
without affecting the original.
So fully:
with engine_or_connection.connect() as conn:
with conn.begin() as trans:
# etc.
On 02/23/2016 11:05 PM, Jonathan Beluch wrote:
Is there a better way of doing this? Basically I have a function that
takes a connectable (engine or connection) and I want to start a
transaction. However I can't just call .begin() because it could return
a Transaction or a Connection depending on what was passed in and I need
a connection.
@contextlib.contextmanager
def _transaction(connectable):
if hasattr(connectable, 'in_transaction') and
connectable.in_transaction():
yield connectable
else:
with connectable.begin() as conn:
yield conn
--
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]
<mailto:[email protected]>.
To post to this group, send email to [email protected]
<mailto:[email protected]>.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.
--
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.