简体   繁体   English

没有事务的python-mysqldb

[英]python-mysqldb without transactions

I'm reading about how do transactions work in python's MySQLdb . 我正在阅读有关事务如何在python的MySQLdb工作的MySQLdb In this tutorial , it says that: 本教程中 ,它说:

In Python DB API, we do not call the BEGIN statement to start a transaction. 在Python DB API中,我们不调用BEGIN语句来启动事务。 A transaction is started when the cursor is created. 创建游标后,将启动一个事务。

So a following line: 所以下面一行:

cur = con.cursor()

starts a transaction implicitly. 隐式启动事务。 It also says, that: 它还说:

We must end a transaction with either a commit() or a rollback() method. 我们必须使用commit()或rollback()方法结束事务。

Do I understand it correctly, that MySQLdb uses transactions always and there's no way to turn this behavior off? 我是否正确理解MySQLdb 始终使用事务并且没有办法关闭此行为? Forcing user to enclose all queries in transactions seems a bit strange. 强制用户将所有查询包含在事务中似乎有些奇怪。 If so - is there any explanation why is that? 如果是这样-有什么解释吗?

I'm not a huge expert in this, but I think the feature you're looking for here is autocommit. 我在这方面不是一个专家,但是我认为您要在此处查找的功能是自动提交的。 This automatically commits your commands. 这将自动提交您的命令。 Therefore you should be able to skip the 'BEGIN' statements. 因此,您应该可以跳过“ BEGIN”语句。

Here's a page on it: http://dev.mysql.com/doc/connector-python/en/connector-python-connectargs.html 这是其上的页面: http : //dev.mysql.com/doc/connector-python/en/connector-python-connectargs.html

You set this up when you start the python MySQLdb instance: 在启动python MySQLdb实例时进行设置:

conn=MySQLdb.connect(host='blah', autocommit=True)

You should then have a connection that doesn't worry about Transactions. 然后,您应该具有不必担心事务的连接。

Some storage engines don't use transactions so if you use one, you won't need to worry about this detail: en.wikipedia.org/wiki/Comparison_of_MySQL_database_engines 一些存储引擎不使用事务,因此,如果您使用事务,则无需担心此细节:en.wikipedia.org/wiki/Comparison_of_MySQL_database_engines

However, they can run into issues if your insert \\ update fails halfway through! 但是,如果您的插入\\更新中途失败,它们可能会遇到问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM