从 SQLAlchemy 0.8.2 开始,您可以BEGIN
在调用时禁用隐式语句create_engine()
engine = create_engine(uri, isolation_level="AUTOCOMMIT")
这种变化有一些微妙的影响。首先,有没有悄悄隐藏在未终止事务中的语句将被悄悄忽略
session.execute("DELETE FROM department WHERE department_id=18")
sys.exit(0)
默认:
LOG: statement: BEGIN
LOG: statement: show standard_conforming_strings
LOG: statement: DELETE FROM department WHERE department_id=18
LOG: unexpected EOF on client connection with an open transaction
自动提交:
LOG: statement: show standard_conforming_strings
LOG: statement: DELETE FROM department WHERE department_id=18
其次,更新多个更新不再是自动的,而rollback()
只是有条件地有效:
department = Department(u"HR")
session.add(department)
session.flush()
employee = Employee(department.department_id, u'Bob')
session.add(employee)
session.rollback()
默认:
LOG: statement: BEGIN
LOG: statement: INSERT INTO department (name) VALUES ('HR') RETURNING department.department_id
LOG: statement: ROLLBACK
自动提交:
LOG: statement: INSERT INTO department (name) VALUES ('HR') RETURNING department.department_id
在 Engine 对象上设置 SQLAlchemyisolation_level
对许多应用程序都很有效。不幸的是,Session.begin()
这并不总是意味着BEGIN TRANSACTION;