Python DB API - commit() v. execute("commit transaction")?

Gregory Ewing greg.ewing at canterbury.ac.nz
Fri Jun 2 21:46:24 EDT 2017


Chris Angelico wrote:
> with psycopg2.connect(...) as conn:
>     with conn.trans() as trn:
>         for row in trn.execute("select ..."):
>             print(row)
> 
> The outer context manager is optional, but not the inner one

While I fully support making the use of transactions mandatory,
I wouldn't like to be forced to use them in a with statement.

In the application that I originally built my Firebird interface
for, I had a mechanism where a user could open up a piece of
data for editing, and then choose to save or cancel the edits.
I implemented it by keeping a transaction around for the
duration and then committing it or rolling it back. If a
with statement were required around all transactions, I
wouldn't have been able to do that.

-- 
Greg



More information about the Python-list mailing list