qqq
This commit is contained in:
+21
-5
@@ -13,6 +13,7 @@ namespace Database;
|
|||||||
use Exception;
|
use Exception;
|
||||||
use Kiri\Abstracts\Component;
|
use Kiri\Abstracts\Component;
|
||||||
use Kiri\Di\Container;
|
use Kiri\Di\Container;
|
||||||
|
use Kiri\Di\Context;
|
||||||
use Kiri\Exception\ConfigException;
|
use Kiri\Exception\ConfigException;
|
||||||
use PDO;
|
use PDO;
|
||||||
use PDOStatement;
|
use PDOStatement;
|
||||||
@@ -26,6 +27,8 @@ use Throwable;
|
|||||||
class Command extends Component
|
class Command extends Component
|
||||||
{
|
{
|
||||||
|
|
||||||
|
const RETRY_NAME = 'db:retry:count';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@@ -86,7 +89,7 @@ class Command extends Component
|
|||||||
return $prepare->fetchAll(PDO::FETCH_ASSOC);
|
return $prepare->fetchAll(PDO::FETCH_ASSOC);
|
||||||
} catch (Throwable $throwable) {
|
} catch (Throwable $throwable) {
|
||||||
$result = $this->error($throwable);
|
$result = $this->error($throwable);
|
||||||
if (str_contains($throwable->getMessage(),'MySQL server has gone away')) {
|
if (str_contains($throwable->getMessage(), 'MySQL server has gone away') && $this->retry()) {
|
||||||
return $this->all();
|
return $this->all();
|
||||||
}
|
}
|
||||||
return $result;
|
return $result;
|
||||||
@@ -110,7 +113,7 @@ class Command extends Component
|
|||||||
return $prepare->fetch(PDO::FETCH_ASSOC);
|
return $prepare->fetch(PDO::FETCH_ASSOC);
|
||||||
} catch (Throwable $throwable) {
|
} catch (Throwable $throwable) {
|
||||||
$result = $this->error($throwable);
|
$result = $this->error($throwable);
|
||||||
if (str_contains($throwable->getMessage(),'MySQL server has gone away')) {
|
if (str_contains($throwable->getMessage(), 'MySQL server has gone away') && $this->retry()) {
|
||||||
return $this->one();
|
return $this->one();
|
||||||
}
|
}
|
||||||
return $result;
|
return $result;
|
||||||
@@ -134,7 +137,7 @@ class Command extends Component
|
|||||||
return $prepare->fetchColumn(PDO::FETCH_ASSOC);
|
return $prepare->fetchColumn(PDO::FETCH_ASSOC);
|
||||||
} catch (Throwable $throwable) {
|
} catch (Throwable $throwable) {
|
||||||
$result = $this->error($throwable);
|
$result = $this->error($throwable);
|
||||||
if (str_contains($throwable->getMessage(),'MySQL server has gone away')) {
|
if (str_contains($throwable->getMessage(), 'MySQL server has gone away') && $this->retry()) {
|
||||||
return $this->fetchColumn();
|
return $this->fetchColumn();
|
||||||
}
|
}
|
||||||
return $result;
|
return $result;
|
||||||
@@ -158,7 +161,7 @@ class Command extends Component
|
|||||||
return $prepare->rowCount();
|
return $prepare->rowCount();
|
||||||
} catch (Throwable $throwable) {
|
} catch (Throwable $throwable) {
|
||||||
$result = $this->error($throwable);
|
$result = $this->error($throwable);
|
||||||
if (str_contains($throwable->getMessage(),'MySQL server has gone away')) {
|
if (str_contains($throwable->getMessage(), 'MySQL server has gone away') && $this->retry()) {
|
||||||
return $this->rowCount();
|
return $this->rowCount();
|
||||||
}
|
}
|
||||||
return $result;
|
return $result;
|
||||||
@@ -168,6 +171,19 @@ class Command extends Component
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
protected function retry(): bool
|
||||||
|
{
|
||||||
|
if (Context::increment(self::RETRY_NAME) < 3) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
Context::remove(self::RETRY_NAME);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return int|bool
|
* @return int|bool
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
@@ -199,7 +215,7 @@ class Command extends Component
|
|||||||
return $result == 0 ? true : (int)$result;
|
return $result == 0 ? true : (int)$result;
|
||||||
} catch (Throwable $throwable) {
|
} catch (Throwable $throwable) {
|
||||||
$result = $this->error($throwable);
|
$result = $this->error($throwable);
|
||||||
if (str_contains($throwable->getMessage(),'MySQL server has gone away')) {
|
if (str_contains($throwable->getMessage(), 'MySQL server has gone away') && $this->retry()) {
|
||||||
return $this->_execute();
|
return $this->_execute();
|
||||||
}
|
}
|
||||||
return $result;
|
return $result;
|
||||||
|
|||||||
Reference in New Issue
Block a user