Actually my example isn't fully correct for the case of passing in a 
connection not in a transaction but the question remains.

On Tuesday, February 23, 2016 at 9:05:03 PM UTC-7, 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 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