[DB-SIG] DB API extension suggestion

Anthony Tuininga anthony.tuininga at gmail.com
Fri Jun 22 15:57:00 CEST 2007


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!

On 6/21/07, Carsten Haese <carsten at uniqsys.com> wrote:
> On Thu, 2007-06-21 at 16:39 -0600, Anthony Tuininga wrote:
> > I have not released this publicly yet, no. It would be a good idea to
> > have agreement on this rather than have conflicting implementations.
>
> Indeed. I see two alternatives, modulo naming:
>
> 1) A connection method:
>
> with conn.transaction(): ...
>
> 2) A module function:
>
> with api.transaction(conn): ...
>
> Implementing 2) would require nothing more than an almost verbatim
> implementation of example 3 from pep-0343, but I personally would prefer
> 1) because it looks cleaner in application code.
>
> As far as the interaction with existing transactions is concerned, I
> think that continuing an existing transaction is the easiest and least
> surprising behavior.
>
> If the module supports autocommit and the connection is in autocommit
> mode, it should leave autocommit mode upon entering the transaction
> block and reenter autocommit mode upon leaving the transaction block.
>
> --
> Carsten Haese
> http://informixdb.sourceforge.net
>
>
>


More information about the DB-SIG mailing list