PDO, mysql, transactions and table locking

后端 未结 3 1052
旧时难觅i
旧时难觅i 2020-12-31 05:00

For fun I am replacing the mysqli extension in my app with PDO.

Once in awhile I need to use transactions + table locking.

In these situations, according to

3条回答
  •  再見小時候
    2020-12-31 05:12

    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.

    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.

    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.

    You can freely mix the PDO API methods of working with transactions and SQL commands when not otherwise working with MySQL's oddities.

提交回复
热议问题