AutoRetryMySqlConnection.php
2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
namespace App\Database\MySQL;
use Closure;
use Illuminate\Database\MySqlConnection;
use Illuminate\Database\QueryException;
use Log;
/**
* Class AutoRetryMySqlConnection
*
* @package App\Helpers
*/
class AutoRetryMySqlConnection extends MySqlConnection
{
/**
* Error code of deadlock exception
*/
const DEADLOCK_ERROR_CODE = 40001;
/**
* Number of attempts to retry
*/
const ATTEMPTS_COUNT = 10;
/**
* Run a SQL statement.
*
* @param string $query
* @param array $bindings
* @param \Closure $callback
* @return mixed
*
* @throws \Illuminate\Database\QueryException
*/
protected function runQueryCallback($query, $bindings, Closure $callback)
{
$attempts_count = self::ATTEMPTS_COUNT;
for ($attempt = 1; $attempt <= $attempts_count; $attempt++) {
try {
return parent::runQueryCallback($query, $bindings, $callback);
} catch (QueryException $e) {
if ($attempt > $attempts_count) {
throw $e;
}
if (!$this->shouldRetry($errorCode = $e->getCode())) {
throw $e;
}
$this->logRetry($attempt, $attempts_count, $bindings, $query, $errorCode);
}
}
}
/**
* Use the provided error code to determine if the transaction should be retried.
*
* @param string|integer $errorCode
*
* @return boolean
*/
protected function shouldRetry($errorCode)
{
return (int)$errorCode === self::DEADLOCK_ERROR_CODE;
}
/**
* Log when a transaction is automatically retried.
*
* @param integer $attempt
* @param integer $attempts_count
* @param array $bindings
* @param string $query
* @param string $errorCode
* @return void
*/
protected function logRetry($attempt, $attempts_count, $bindings, $query, $errorCode)
{
Log::warning("Transaction has been restarted due to error {$errorCode}. Attempt {$attempt}/{$attempts_count}. SQL: {$query} " . print_r($bindings, true));
}
}