Skip to content

Commit ad72023

Browse files
committed
Fix mysql gone away issue - #1241 #1221 #1220
1 parent b5333fd commit ad72023

File tree

6 files changed

+70
-13
lines changed

6 files changed

+70
-13
lines changed

src/Driver/AbstractDriver.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,21 @@ public function getConnection(bool $keep = false): ConnectionInterface
170170
return $conn;
171171
}
172172

173+
public function dropConnection(?ConnectionInterface $conn = null): static
174+
{
175+
if ($conn) {
176+
$conn->release();
177+
178+
$this->getPool()->dropConnection($conn);
179+
} elseif ($this->connection) {
180+
$this->getPool()->dropConnection(
181+
$this->releaseKeptConnection()
182+
);
183+
}
184+
185+
return $this;
186+
}
187+
173188
public function releaseKeptConnection(): ?ConnectionInterface
174189
{
175190
if ($this->connection) {
@@ -209,16 +224,18 @@ public function useConnection(callable $callback): mixed
209224
return $result;
210225
}
211226

212-
/**
213-
* disconnect
214-
*
215-
* @return int
216-
*/
217227
public function disconnectAll(): int
218228
{
219229
return $this->getPool()->close();
220230
}
221231

232+
public function disconnect(): static
233+
{
234+
$this->dropConnection();
235+
236+
return $this;
237+
}
238+
222239
/**
223240
* createStatement
224241
*

src/Driver/AbstractStatement.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,46 @@ public function execute(?array $params = null): static
169169
*/
170170
abstract protected function doExecute(?array $params = null): bool;
171171

172+
protected function tryExecute(\Closure $handler)
173+
{
174+
return $this->driver->useConnection(
175+
function (ConnectionInterface $conn) use ($handler) {
176+
try {
177+
return $handler($conn);
178+
} catch (\Exception $e) {
179+
if (!$this->shouldReconnect($e)) {
180+
throw $e;
181+
}
182+
183+
$conn->reconnect();
184+
185+
return $handler($conn);
186+
}
187+
}
188+
);
189+
}
190+
191+
protected function shouldReconnect(\Throwable $e): bool
192+
{
193+
$keywords = [
194+
'has gone away',
195+
'lost connection',
196+
'went away',
197+
'connection timed out',
198+
'operation timed out'
199+
];
200+
201+
$message = strtolower($e->getMessage());
202+
203+
foreach ($keywords as $keyword) {
204+
if (str_contains($message, strtolower($keyword))) {
205+
return true;
206+
}
207+
}
208+
209+
return false;
210+
}
211+
172212
/**
173213
* @inheritDoc
174214
*/

src/Driver/Mysqli/MysqliStatement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static function ($param) {
5656

5757
[$query, $params] = BoundedHelper::replaceParams($this->query, '?', $params);
5858

59-
$this->driver->useConnection(
59+
$this->tryExecute(
6060
function (ConnectionInterface $conn) use ($params, $query) {
6161
$this->conn = $conn->get();
6262
$this->cursor = $stmt = $this->conn->prepare($query);

src/Driver/Pdo/PdoStatement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class PdoStatement extends AbstractStatement
3232
*/
3333
protected function doExecute(?array $params = null): bool
3434
{
35-
return $this->driver->useConnection(
35+
return $this->tryExecute(
3636
function (ConnectionInterface $conn) use ($params) {
3737
/** @var PDO $pdo */
3838
$this->conn = $pdo = $conn->get();

src/Driver/Pgsql/PgsqlStatement.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static function ($param) {
4747

4848
[$query, $params] = BoundedHelper::replaceParams($this->query, '$%d', $params);
4949

50-
$this->driver->useConnection(
50+
$this->tryExecute(
5151
function (ConnectionInterface $conn) use ($params, $query) {
5252
$this->conn = $resource = $conn->get();
5353

@@ -60,13 +60,13 @@ function (ConnectionInterface $conn) use ($params, $query) {
6060
}
6161

6262
$this->cursor = pg_execute($resource, $stname, $args);
63+
64+
if (!$this->cursor) {
65+
throw new StatementException(pg_last_error());
66+
}
6367
}
6468
);
6569

66-
if (!$this->cursor) {
67-
throw new StatementException(pg_last_error());
68-
}
69-
7070
return true;
7171
}
7272

src/Driver/Sqlsrv/SqlsrvStatement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ static function ($param) {
5252
$args[] = &$param['value'];
5353
}
5454

55-
return $this->driver->useConnection(
55+
return $this->tryExecute(
5656
function (ConnectionInterface $conn) use ($args, $query) {
5757
$this->conn = $resource = $conn->get();
5858

0 commit comments

Comments
 (0)