Why connection in Python's DB-API does not have “begin” operation?

瘦欲@ 提交于 2019-12-03 13:10:15

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

cursor = conn.cursor()
try:
    cursor.execute(...)
except DatabaseError:
    conn.rollback()
    raise
else:
    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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!