PHP的死锁异常代码,MySQL PDOException?

问题描述 投票:2回答:1

使用PHP PDO MySQL,异常模式。

是否存在[MySQL]死锁的PDOException代码?

如果不是,还有其他选择吗?

php mysql pdo deadlock
1个回答
8
投票

PDO::errorInfo, PDOException::errorInfo

MySQL Server Error Codes and Messages; Error: 1213 SQLSTATE: 40001 (ER_LOCK_DEADLOCK)

/*[...]*/

//error mode is exception
$pdoDBHandle->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);

try
{
    /*[...]*/
}
catch(\PDOException $exc)
{
    if(
        $exc->errorInfo[0]==40001 /*(ISO/ANSI) Serialization failure, e.g. timeout or deadlock*/;

        && $pdoDBHandle->getAttribute(\PDO::ATTR_DRIVER_NAME)=="mysql"
        && $exc->errorInfo[1]==1213  /*(MySQL SQLSTATE) ER_LOCK_DEADLOCK*/
    )
    {
        /*[...]*/
    }
    else
        throw $exc;
}
© www.soinside.com 2019 - 2024. All rights reserved.