python - 默认情况下 DB API 2.0 自动提交关闭的理由?

标签 python transactions python-db-api

PEP 249 -- Python Database API Specification v2.0.commit() 的描述中状态:

Note that if the database supports an auto-commit feature, this must be initially off. An interface method may be provided to turn it back on.

鉴于大多数数据库默认为自动提交,其背后的基本原理是什么?

最佳答案

根据 Discovering SQL :

The transaction model, as it is defined in the ANSI/ISO SQL Standard, utilizes the implicit start of a transaction, with an explicit COMMIT, in the case of the successful execution of all the logical units of the transaction, or an explicit ROLLBACK, when the noncommitted changes need to be rolled back (for example, when the program terminates abnormally); most RDBMSs follow this model.

即,SQL 标准规定事务应该显式提交或 回滚。

SQL-Transactions 最好地描述了显式提交的情况。 :

Some DBMS products, for example, SQL Server, MySQL/InnoDB, PostgreSQL and Pyrrho operate by default in the AUTOCOMMIT mode. This means that the result of every single SQL command will is automatically committed to the database, thus the effects/changes made to the database by the statement in question cannot be rolled back. So, in case of errors the application needs do reverse-operations for the logical unit of work, which may be impossible after operations of concurrent SQL-clients. Also in case of broken connections the database might be left in inconsistent state.

即,错误处理和操作的逆转在使用时可以简单得多 使用显式提交而不是自动提交。

此外,根据我对 python 邮件列表中用户的观察, 共识是默认情况下自动提交是不好的。

One post状态:

Auto commit is a bad thing and a pretty evil invention of ODBC. While it does make writing ODBC drivers simpler (ones which don't support transactions that is), it is potentially dangerous at times, e.g. take a crashing program: there is no way to recover from errors because the database has no way of knowing which data is valid and which is not. No commercial application handling "mission critical" (I love that term ;-) data would ever want to run in auto-commit mode.

Another post说:

ANY serious application MUST manage its own transactions, as otherwise you can't ever hope to control failure modes.

我的印象是 Python 开发人员考虑了这类信息,并认为默认情况下关闭自动提交(更容易处理错误和逆转)的好处超过了启用自动提交(增加并发性)的好处。

关于python - 默认情况下 DB API 2.0 自动提交关闭的理由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22019154/

相关文章:

python - 如何为时间戳(日期时间)的数组指定 psycopg2 参数

python - 如何使用分而治之的多线程?

python - 如何创建一个函数来检测字符串中的字符并返回它在字符串中的位置? Python 3.2

spring - 使用 Spring Data Neo4j 的简单对象/图形映射获取事务的正确方法?

sql-server - 是否可以使用事务创建 SQL SERVER 函数

Python调用带有表值参数的sql-server存储过程

python - 如何从 Azure ML 中的 python 脚本获取 Web 服务的输出

python - 如何将所有白色像素的坐标附加到数组中? OpenCV Python

javascript - firebase 交易减少值

python - 值错误 : operation parameter must be str