[DB-SIG] DB API extension suggestion

Robert Brewer fumanchu at amor.org
Fri Jun 22 18:12:23 CEST 2007


Anthony Tuininga wrote:
> Hmm, I've thought about this a bit more. If you look at the module
> contextlib in the Python 2.5 standard library you can see that making
> connections and cursors context managers that simply close themselves
> is redundant. You can do this instead:
> 
> with contextlib.closing(connection.cursor()) as cursor:
>     do_stuff()
> 
> I think it makes more sense to define database connections as context
> managers that deal with transactions since that is the primary purpose
> of a database connection -- not opening and closing itself!

Wha? There are plenty of good DB architectures that don't use
transactions. They still need connections. Let's not conflate the two
any more than the DB-API already does. Note also that, "[conn.rollback]
is optional since not all databases provide transaction support." What
happens to "with conn" (as transaction manager) if the DB has no
transaction support?

+1 for "with connection" implementing close(), and "with
connection.transaction()" implementing start+commit/rollback if the DB
supports it.


Robert Brewer
System Architect
Amor Ministries
fumanchu at amor.org


More information about the DB-SIG mailing list