PHP PDO MySQL 以及它如何真正处理 MySQL 事务?
Posted
技术标签:
【中文标题】PHP PDO MySQL 以及它如何真正处理 MySQL 事务?【英文标题】:PHP PDO MySQL and how does it really deal with MySQL transactions? 【发布时间】:2018-05-28 08:26:29 【问题描述】:我正在努力克服它,但我就是无法理解在 php 中使用 PDO 和 mysql 进行事务处理背后的逻辑。
我知道这个问题会很长,但我认为这是值得的。
鉴于我阅读了很多关于 MySQL 事务、服务器如何处理它们、它们如何与锁和其他隐式提交语句等相关的内容,不仅在 SO 上,而且在 MySQL 和 PHP 手册上也有:
Mysql transactions within transactions Difference between SET autocommit=1 and START TRANSACTION in mysql (Have I missed something?) https://dev.mysql.com/doc/refman/5.7/en/commit.html https://dev.mysql.com/doc/refman/5.7/en/lock-tables-and-transactions.html http://php.net/manual/en/pdo.transactions.php http://php.net/manual/en/pdo.begintransaction.php并给出以下代码:
架构:
CREATE TABLE table_name (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
table_col VARCHAR(100) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `another_table` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`another_col` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
test1.php(带有PDO::setAttribute(PDO::ATTR_AUTOCOMMIT, 0)
):
<?php
// PDO
define('DB_HOST', 'localhost');
define('DB_USER', 'user');
define('DB_PASS', 'password');
define('DB_NAME', 'db_name');
/**
* Uses `$this->pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,0);`
*/
class Database
private $host = DB_HOST;
private $user = DB_USER;
private $pass = DB_PASS;
private $dbname = DB_NAME;
private $pdo;
public $error;
private $stmt;
public function __construct($host=NULL,$user=NULL,$pass=NULL,$dbname=NULL)
if ($host!==NULL)
$this->host=$host;
if ($user!==NULL)
$this->user=$user;
if ($pass!==NULL)
$this->pass=$pass;
if ($dbname!==NULL)
$this->dbname=$dbname;
// Set DSN
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
// Set options
$options = array(
PDO::ATTR_PERSISTENT => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// Create a new PDO instanace
$this->pdo = new PDO($dsn, $this->user, $this->pass, $options);
$this->pdo->exec("SET NAMES 'utf8'");
public function cursorClose()
$this->stmt->closeCursor();
public function close()
$this->pdo = null;
$this->stmt = null;
return true;
public function beginTransaction()
$this->pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,0);
return $this->pdo->beginTransaction();
public function commit()
$ok = $this->pdo->commit();
$this->pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,1);
return $ok;
public function rollback()
$ok = $this->pdo->rollback();
$this->pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,1);
return $ok;
public function bind($param, $value, $type = null)
if (is_null($type))
switch (true)
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
$this->stmt->bindValue($param, $value, $type);
public function runquery()
$this->stmt->execute();
public function execute($nameValuePairArray = NULL)
try
if (is_array($nameValuePairArray) && !empty($nameValuePairArray))
return $this->stmt->execute($nameValuePairArray);
else
return $this->stmt->execute();
catch(PDOException $e)
$this->error = $e->getMessage();
return FALSE;
public function lastInsertId()
return $this->pdo->lastInsertId();
public function insert($table, $data)
if (!empty($data))
$fields = "";
$values = "";
foreach($data as $field => $value)
if ($fields=="")
$fields = "$field";
$values = ":$field";
else
$fields .= ",$field";
$values .= ",:$field";
$query = "INSERT INTO $table ($fields) VALUES ($values) ";
$this->query($query);
foreach($data as $field => $value)
$this->bind(":$field",$value);
if ($this->execute()===FALSE)
return FALSE;
else
return $this->lastInsertId();
$this->error = "No fields during insert";
return FALSE;
public function query($query)
$this->stmt = $this->pdo->prepare($query);
public function setBuffered($isBuffered=false)
$this->pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, $isBuffered);
public function lockTables($tables)
$query = "LOCK TABLES ";
foreach($tables as $table=>$lockType)
$query .= "$table $lockType, ";
$query = substr($query,0, strlen($query)-2);
$this->query($query);
return $this->execute();
public function unlockTables()
$query = "UNLOCK TABLES";
$this->query($query);
return $this->execute();
$db = NULL;
try
$db = new Database();
$db->beginTransaction();
// If I call `LOCK TABLES` here... No implicit commit. Why?
// Does `$this->pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,0);` prevent it?
$db->lockTables(array('another_table' => 'WRITE'));
$db->insert('another_table', array('another_col' => 'TEST1_ANOTHER_TABLE'));
$db->unlockTables();
// If I insert a row, other MySQL clients do not see it. Why?
// I called `LOCK TABLES` above and as the MySQL manual says:
//
// LOCK TABLES is not transaction-safe and implicitly commits any active transaction before attempting to lock the tables.
//
$db->insert('table_name', array('table_col' => 'TEST1_TABLE_NAME'));
//...
// If I rollback for some reason, everything rolls back, but shouldn't the transaction
// be already committed with the initial `LOCK TABLES`?
// So I should expect to get a PDOException like "There's no active transaction" or something similar, shouldn't I?
//$db->rollback();
// If I commit instead of the above `$db->rollback()` line, everything is committed, but only now other clients see the new row in `table_name`,
// not straightforward as soon I called `$db->insert()`, whereas I guess they should have seen the change
// even before the following line because I am using `LOCK TABLES` before (see `test2.php`).
$db->commit();
catch (PDOException $e)
echo $e->getMessage();
if (!is_null($db))
$db->close();
test2.php(没有PDO::setAttribute(PDO::ATTR_AUTOCOMMIT, 0)
行的数据库(已注释掉)):
<?php
// PDO
define('DB_HOST', 'localhost');
define('DB_USER', 'user');
define('DB_PASS', 'password');
define('DB_NAME', 'db_name');
/**
* Does not use `$this->pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,0);`
*/
class Database
private $host = DB_HOST;
private $user = DB_USER;
private $pass = DB_PASS;
private $dbname = DB_NAME;
private $pdo;
public $error;
private $stmt;
public function __construct($host=NULL,$user=NULL,$pass=NULL,$dbname=NULL)
if ($host!==NULL)
$this->host=$host;
if ($user!==NULL)
$this->user=$user;
if ($pass!==NULL)
$this->pass=$pass;
if ($dbname!==NULL)
$this->dbname=$dbname;
// Set DSN
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
// Set options
$options = array(
PDO::ATTR_PERSISTENT => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// Create a new PDO instanace
$this->pdo = new PDO($dsn, $this->user, $this->pass, $options);
$this->pdo->exec("SET NAMES 'utf8'");
public function cursorClose()
$this->stmt->closeCursor();
public function close()
$this->pdo = null;
$this->stmt = null;
return true;
public function beginTransaction()
//$this->pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,0);
return $this->pdo->beginTransaction();
public function commit()
$ok = $this->pdo->commit();
//$this->pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,1);
return $ok;
public function rollback()
$ok = $this->pdo->rollback();
//$this->pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,1);
return $ok;
public function bind($param, $value, $type = null)
if (is_null($type))
switch (true)
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
$this->stmt->bindValue($param, $value, $type);
public function runquery()
$this->stmt->execute();
public function execute($nameValuePairArray = NULL)
try
if (is_array($nameValuePairArray) && !empty($nameValuePairArray))
return $this->stmt->execute($nameValuePairArray);
else
return $this->stmt->execute();
catch(PDOException $e)
$this->error = $e->getMessage();
return FALSE;
public function lastInsertId()
return $this->pdo->lastInsertId();
public function insert($table, $data)
if (!empty($data))
$fields = "";
$values = "";
foreach($data as $field => $value)
if ($fields=="")
$fields = "$field";
$values = ":$field";
else
$fields .= ",$field";
$values .= ",:$field";
$query = "INSERT INTO $table ($fields) VALUES ($values) ";
$this->query($query);
foreach($data as $field => $value)
$this->bind(":$field",$value);
if ($this->execute()===FALSE)
return FALSE;
else
return $this->lastInsertId();
$this->error = "No fields during insert";
return FALSE;
public function query($query)
$this->stmt = $this->pdo->prepare($query);
public function setBuffered($isBuffered=false)
$this->pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, $isBuffered);
public function lockTables($tables)
$query = "LOCK TABLES ";
foreach($tables as $table=>$lockType)
$query .= "$table $lockType, ";
$query = substr($query,0, strlen($query)-2);
$this->query($query);
return $this->execute();
public function unlockTables()
$query = "UNLOCK TABLES";
$this->query($query);
return $this->execute();
$db = NULL;
try
$db = new Database();
$db->beginTransaction();
// If I call `LOCK TABLES` here... There's an implicit commit.
$db->lockTables(array('another_table' => 'WRITE'));
$db->insert('another_table', array('another_col' => 'TEST2_ANOTHER_TABLE'));
$db->unlockTables();
// If I insert a row, other MySQL clients see it straightforward (no need to reach `$db->commit()`).
// This is coherent with the MySQL manual:
//
// LOCK TABLES is not transaction-safe and implicitly commits any active transaction before attempting to lock the tables.
//
$db->insert('table_name', array('table_col' => 'TEST2_TABLE_NAME'));
//...
// If I rollback for some reason, the row does not rollback, as the transaction
// was already committed with the initial `LOCK TABLES` statement above.
//
// I cannot rollback the insert into table `table_name`
//
// So I should expect to get a PDOException like "There's no active transaction" or something similar, shouldn't I?
$db->rollback();
// If I commit instead of the above `$db->rollback()` line, I guess nothing happens, because the transaction
// was already committed and as I said above, and clients already saw the changes before this line was reached.
// Again, this is coherent with the MySQL statement:
//
// LOCK TABLES is not transaction-safe and implicitly commits any active transaction before attempting to lock the tables.
//
//$db->commit();
catch (PDOException $e)
echo $e->getMessage();
if (!is_null($db))
$db->close();
我还有以下疑惑和未解决的问题:
使用InnoDB
,有区别吗
PDO::beginTransaction()
和 PDO::setAttribute(PDO::ATTR_AUTOCOMMIT, 0)
当我们在 PHP 和/或 MySQL 中使用 PDO
时使用纯 MySQL 语句 SET AUTOCOMMIT = 0;
和 START TRANSACTION;
?如果是,那是什么?
如果您查看我的 PHP 示例,在 Database::beginTransaction()
包装器方法中,我在文件 test1.php 中同时使用 PDO::beginTransaction()
和 PDO::setAttribute(PDO::ATTR_AUTOCOMMIT, 0)
,并且在文件 中不使用 PDO::setAttribute(PDO::ATTR_AUTOCOMMIT, 0)
>test2.php。
我发现当我使用PDO::setAttribute(PDO::ATTR_AUTOCOMMIT, 0)
时会发生奇怪的事情:
在Database
(test1.php) 中有PDO::setAttribute(PDO::ATTR_AUTOCOMMIT, 0)
行,在一个
带有LOCK TABLES
语句的事务,LOCK TABLES
不
似乎隐式提交事务,因为如果我连接
对于另一个客户端,在代码到达$db->commit();
行之前,我看不到插入的行,而 MySQL
手册说:
LOCK TABLES 不是事务安全的,它会在尝试锁定表之前隐式提交任何活动事务。
因此我们可以这样说PDO::setAttribute(PDO::ATTR_AUTOCOMMIT, 0)
(在 MySQL 上是
SET AUTOCOMMIT = 0;
) 事务没有被隐式提交
像LOCK TABLES
这样的陈述?然后我会说有一个
MySQL手册与PHP PDO实现不一致
(我不是在抱怨,我只是想了解);
如果Database
(test2.php) 中没有 PDO::setAttribute(PDO::ATTR_AUTOCOMMIT, 0)
行,代码的行为似乎与 MySQL 的一致
手动LOCK TABLES is not transaction-safe and implicitly
commits any active transaction before attempting to lock the
tables.
:一旦到达LOCK TABLES
查询,就会有一个隐式提交,所以在$db->insert('table_name', array('table_col' => 'TEST2_TABLE_NAME'));
行之后,其他客户端甚至可以在到达$db->commit();
之前看到新插入的行;
我刚才描述的以下行为的解释是什么?当我们使用 PHP 的 PDO
并在我们的事务中包含 implicit-commit
语句时,事务如何工作?
我的PHP版本是7.0.22
,MySQL版本是5.7.20
。
感谢您的关注。
【问题讨论】:
***.com/questions/3106737/… 看起来内容丰富。根据PDO::beginTransaction
实际上是关闭自动提交而不是开始新事务。这种说法似乎与这种行为一致
但是有什么区别呢?如果PDO::beginTransaction
关闭了自动提交,那为什么我写的2个脚本的结果根据我是否使用$this->pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,0);
而不同呢?
【参考方案1】:
https://dev.mysql.com/doc/refman/5.7/en/innodb-autocommit-commit-rollback.html 说:
如果在 SET autocommit = 0 的会话中禁用自动提交模式,则该会话始终打开一个事务。 COMMIT 或 ROLLBACK 语句结束当前事务并开始新事务。
因此,当您在会话中设置 autocommit=0(称为会话 1)时,这会隐式打开一个事务,并使其无限期地保持打开状态。
默认事务隔离级别是 REPEATABLE-READ。因此,在会话 1 明确提交或回滚之前,您的会话将不会看到来自其他会话工作的已提交更改的刷新视图。
您在另一个会话 2 中的 LOCK TABLES 确实会导致隐式提交,但会话 1 看不到结果,因为由于它自己的事务,它仍然只能看到数据的隔离视图快照。
【讨论】:
感谢您的回复。在我的情况下,会话 1 是设置autocommit = 0
并使用 LOCK TABLES
(不是会话 2)的会话。在这种情况下session 2
会发生什么?会话 1 中 LOCK 之前的所有内容都将被提交,另一个事务直接开始,但会话 2 直到有明确的 COMMIT/ROLLBACK
语句才能看到更改?
@tonix,我建议你打开两个终端窗口并在每个窗口中运行mysql
客户端。然后你可以轻松地做一些实验。
我按照你说的做了,似乎当AUTOCOMMIT=0
时,你的声明A COMMIT or ROLLBACK statement ends the current transaction and a new one starts.
也适用于像LOCK TABLES
这样的隐式提交声明。对吗?
是的,应该是正确的。当 autocommit=0 时,您始终有一个事务处于打开状态,这意味着一旦提交了一个事务,就会开始一个新事务。
所以如果你COMMIT
、ROLLBACK
或者发出像LOCK TABLES
这样的隐式提交语句,MySQL就会自动为你执行START TRANSACTION
,对吧?以上是关于PHP PDO MySQL 以及它如何真正处理 MySQL 事务?的主要内容,如果未能解决你的问题,请参考以下文章