Skip to content Skip to sidebar Skip to footer

Why Connection In Python's Db-api Does Not Have "begin" Operation?

Working with cursors in mysql-python I used to call 'BEGIN;', 'COMMIT;', and 'ROLLBACK;' explicitly as follows: try: cursor.execute('BEGIN;') # some statements cursor.e

Solution 1:

look a this previously asked question. Generally the "protocol" to use with transactions is:

cursor = conn.cursor()
try:
    cursor.execute(...)
except DatabaseError:
    conn.rollback()
    raiseelse:
    conn.commit()
finally:
    cursor.close()

Starting from python 2.6 sqlite Connection objects can be used as context managers that automatically commit or rollback transactions.

Solution 2:

Decided to answer myself:

A thread about DB API 2.0 transactions in python-list and the following excerpt from the noticeable book SQL The Complete Reference make me think that DB API implements SQL1 standard behaviour:

The first version of the SQL standard (SQL1) defined an implicit transaction mode, based on the transaction support in the early releases of DB2. In implicit mode, only the COMMIT and ROLLBACK statements are supported. A SQL transaction automatically begins with the first SQL statement executed by a user or a program and ends when a COMMIT or ROLLBACK is executed. The end of one transaction implicitly starts a new one.

Explicit transaction mode (the SQL2 and SQL:1999) seems to be handy when the RDBSM supports autocommit mode and the current connection is in that mode, but DB API just does not reflect it.

Post a Comment for "Why Connection In Python's Db-api Does Not Have "begin" Operation?"