| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Database;
|
|
|
4 |
|
|
|
5 |
use Illuminate\Support\Str;
|
|
|
6 |
use PDOException;
|
|
|
7 |
use Throwable;
|
|
|
8 |
|
|
|
9 |
trait DetectsConcurrencyErrors
|
|
|
10 |
{
|
|
|
11 |
/**
|
|
|
12 |
* Determine if the given exception was caused by a concurrency error such as a deadlock or serialization failure.
|
|
|
13 |
*
|
|
|
14 |
* @param \Throwable $e
|
|
|
15 |
* @return bool
|
|
|
16 |
*/
|
|
|
17 |
protected function causedByConcurrencyError(Throwable $e)
|
|
|
18 |
{
|
|
|
19 |
if ($e instanceof PDOException && ($e->getCode() === 40001 || $e->getCode() === '40001')) {
|
|
|
20 |
return true;
|
|
|
21 |
}
|
|
|
22 |
|
|
|
23 |
$message = $e->getMessage();
|
|
|
24 |
|
|
|
25 |
return Str::contains($message, [
|
|
|
26 |
'Deadlock found when trying to get lock',
|
|
|
27 |
'deadlock detected',
|
|
|
28 |
'The database file is locked',
|
|
|
29 |
'database is locked',
|
|
|
30 |
'database table is locked',
|
|
|
31 |
'A table in the database is locked',
|
|
|
32 |
'has been chosen as the deadlock victim',
|
|
|
33 |
'Lock wait timeout exceeded; try restarting transaction',
|
|
|
34 |
'WSREP detected deadlock/conflict and aborted the transaction. Try restarting the transaction',
|
|
|
35 |
]);
|
|
|
36 |
}
|
|
|
37 |
}
|