Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deadlock exception code for PHP, MySQL PDOException?

Using PHP PDO MySQL, exception mode.

Does a PDOException code for [MySQL] deadlocks exist?

Is there an alternative if it doesn't?

like image 308
Tiberiu-Ionuț Stan Avatar asked Dec 12 '22 22:12

Tiberiu-Ionuț Stan


1 Answers

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;
}
like image 115
Tiberiu-Ionuț Stan Avatar answered Dec 25 '22 19:12

Tiberiu-Ionuț Stan