This commit is contained in:
2023-08-17 10:16:08 +08:00
parent 37abaa8035
commit ccacfcbc02
+5 -21
View File
@@ -13,11 +13,8 @@ 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 ReflectionException;
use Throwable; use Throwable;
/** /**
@@ -89,7 +86,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') && $this->retry()) { if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) {
return $this->all(); return $this->all();
} }
return $result; return $result;
@@ -113,7 +110,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') && $this->retry()) { if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) {
return $this->one(); return $this->one();
} }
return $result; return $result;
@@ -137,7 +134,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') && $this->retry()) { if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) {
return $this->fetchColumn(); return $this->fetchColumn();
} }
return $result; return $result;
@@ -161,7 +158,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') && $this->retry()) { if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) {
return $this->rowCount(); return $this->rowCount();
} }
return $result; return $result;
@@ -171,19 +168,6 @@ 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
@@ -215,7 +199,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') && $this->retry()) { if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) {
return $this->_execute(); return $this->_execute();
} }
return $result; return $result;