Handling transactions in Python DBI module

Chris Angelico rosuav at gmail.com
Thu Feb 11 03:50:10 EST 2016


On Thu, Feb 11, 2016 at 6:59 PM, dieter <dieter at handshake.de> wrote:
> In my context (web applications), I strongly discourage this use - at least
> when "conn" does not handle subtransactions properly.
>
> In a web application, the main transaction should in general be controlled
> at the request level: a request should either succeed as a whole or have no
> side effects whatsoever. This prohibits local (somewhere
> deep in a component) transaction control.

Hmm. I'm not 100% convinced that web apps should behave that way; but
part of the simplicity comes from requiring that database-dependent
code should not call other database-dependent code, as that would
create a nested transaction. (That said, though, it's entirely
possible that psycopg2 could handle a nested "with conn" as
"SAVEPOINT" and either "RELEASE SAVEPOINT" or "ROLLBACK TO SAVEPOINT".
But I wouldn't recommend depending on that without confirming it in
the docs.)

Bear in mind, the rule I gave was a broad and general rule for
students to follow, not a hard-and-fast rule for all databasing. It's
designed such that the student can learn the exceptions later on, but
meanwhile, his code will be correct (if occasionally a little warped
to avoid nesting transactions).

ChrisA


More information about the Python-list mailing list