[DB-SIG] autocommit support in pep-249

M.-A. Lemburg mal at egenix.com
Wed Sep 14 20:38:15 CEST 2011

Joshua D. Drake wrote:
> On 09/14/2011 10:25 AM, Michael Bayer wrote:
>> On Sep 14, 2011, at 12:56 PM, Joshua D. Drake wrote:
>>> No. What I am saying is, the begin() is optional. If you look at my
>>> example, if PostgreSQL is in autocommit mode, the only way to start a
>>> transaction is to execute a query to start the transaction (BEGIN or
>>> START TRANSACTION). From a, "it would be nice if" perspective, I
>>> would like to have the option of using .begin() to start a
>>> transaction. That seems to jive better with proper coding.
>> "proper" on postgresql sure.  On Oracle, there is no BEGIN.   I like
>> that the DBAPI tries to simplify this disconnect with a consistent,
>> moderately coarse-grained facade rather than confusing users and
>> burdening DBAPI authors with two ways to do it.
> Thus the "optional". Those that can make use of it can, those that don't
> don't need it or it will be silently ignored.

I find the following is more readable (explicit better then
implicit etc.):

    connection = connect(dbname, uid, pwd)

    # Regular user code:
    ... transactional code ...

    # Experts only section:
        connection.autocommit = True
        ... non-transactional code ...
        connection.autocommit = False

    # Regular user code:
    ... transactional code ...


You usually only need autocommit code in case you are dealing
with databases that cannot handle database definition SQL (DDL)
inside transactions (e.g. create/drop databases or tables),
if you have a need for atomic operations, or need to
avoid locking for some special reason.

In practice, it's often better to open a separate connection
in autocommit mode, than to switch modes on a single connection.

Marc-Andre Lemburg

Professional Python Services directly from the Source  (#1, Sep 14 2011)
>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
2011-10-04: PyCon DE 2011, Leipzig, Germany                20 days to go

::: Try our new mxODBC.Connect Python Database Interface for free ! ::::

   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611

More information about the DB-SIG mailing list