问题描述
为了娱乐,我将我的应用程序中的mysqli扩展替换为PDO.
For fun I am replacing the mysqli extension in my app with PDO.
一段时间后,我需要使用事务+表锁定.
Once in awhile I need to use transactions + table locking.
在这种情况下,根据mysql手册,语法需要有所不同.您不必这样调用START TRANSACTION,而是这样做...
In these situations, according to the mysql manual, the syntax needs to be a bit different. Instead of calling START TRANSACTION, you do it like so...
SET autocommit=0;
LOCK TABLES t1 WRITE, t2 READ, ...;
... do something with tables t1 and t2 here ...
COMMIT;
UNLOCK TABLES;
( http://dev.mysql .com/doc/refman/5.0/en/lock-tables-and-transactions.html )
我的问题是,这如何与PDO :: beginTransaction交互?在这种情况下可以使用PDO :: beginTransaction吗?还是应该手动发送sql"SET autocommit = 0; ...等等".
My question is, how does this interact with PDO::beginTransaction? Can I use PDO::beginTransaction in this case? Or should I manually send the sql "SET autocommit = 0; ... etc".
感谢您的建议,
推荐答案
在MySQL中,由于LOCK/UNLOCK TABLES的工作方式,开始事务与关闭自动提交不同.在MySQL中,LOCK TABLES会提交所有打开的事务,但是关闭autocommit实际上并不会启动事务. MySQL很有趣.
In MySQL, beginning a transaction is different than turning off autocommit, due to how LOCK/UNLOCK TABLES works. In MySQL, LOCK TABLES commits any open transactions, but turning off autocommit isn't actually starting a transaction. MySQL is funny that way.
在PDO中,使用 beginTransaction
开始事务实际上并没有开始新事务,它只是关闭自动提交.在大多数数据库中,这是理智的,但是它可能会对MySQL提到的行为产生副作用.
In PDO, starting a transaction using beginTransaction
doesn't actually start a new transaction, it just turns off autocommit. In most databases, this is sane, but it can have side effects with MySQL's mentioned behavior.
您可能不应该依赖此行为以及它如何与MySQL的怪癖进行交互.如果要处理MySQL的表锁定和DDL行为,则应避免使用它.如果要关闭自动提交功能,请手动将其关闭.如果要打开交易,请手动打开交易.
You probably should not rely on this behavior and how it interacts with MySQL's quirks. If you're going to deal with MySQL's behavior for table locking and DDL, you should avoid it. If you want autocommit off, turn it off by hand. If you want to open a transaction, open a transaction by hand.
如果不使用MySQL的奇怪特性,则可以自由地混合使用PDO API处理事务和SQL命令的方法.
You can freely mix the PDO API methods of working with transactions and SQL commands when not otherwise working with MySQL's oddities.
这篇关于PDO,mysql,事务和表锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!