Skip to content

Commit e5a30b0

Browse files
committed
wip
1 parent 766776f commit e5a30b0

File tree

8 files changed

+39
-16
lines changed

8 files changed

+39
-16
lines changed

system/Database/Config.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,7 @@ public static function validateForWorkerMode(): void
167167
}
168168

169169
foreach (static::$instances as $connection) {
170-
if (! $connection->ping()) {
171-
$connection->reconnect();
172-
}
170+
$connection->reconnect();
173171
}
174172
}
175173

system/Database/MySQLi/Connection.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,10 @@ public function connect(bool $persistent = false)
257257
*/
258258
public function reconnect()
259259
{
260-
$this->close();
261-
$this->initialize();
260+
if ($this->ping() === false) {
261+
$this->close();
262+
$this->initialize();
263+
}
262264
}
263265

264266
/**

system/Database/OCI8/Connection.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ public function connect(bool $persistent = false)
158158
*/
159159
public function reconnect()
160160
{
161+
if ($this->ping() === false) {
162+
$this->close();
163+
$this->initialize();
164+
}
161165
}
162166

163167
/**
@@ -176,6 +180,20 @@ protected function _close()
176180
oci_close($this->connID);
177181
}
178182

183+
/**
184+
* Ping the database connection.
185+
*/
186+
protected function _ping(): bool
187+
{
188+
try {
189+
$result = $this->simpleQuery('SELECT 1 FROM DUAL');
190+
191+
return $result !== false;
192+
} catch (DatabaseException) {
193+
return false;
194+
}
195+
}
196+
179197
/**
180198
* Select a specific database table to use.
181199
*/

system/Database/Postgre/Connection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ private function convertDSN()
150150
*/
151151
public function reconnect()
152152
{
153-
if ($this->connID === false || pg_ping($this->connID) === false) {
153+
if ($this->ping() === false) {
154154
$this->close();
155155
$this->initialize();
156156
}

system/Database/SQLSRV/Connection.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,10 @@ public function getAllErrorMessages(): string
174174
*/
175175
public function reconnect()
176176
{
177-
$this->close();
178-
$this->initialize();
177+
if ($this->ping() === false) {
178+
$this->close();
179+
$this->initialize();
180+
}
179181
}
180182

181183
/**

system/Database/SQLite3/Connection.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,10 @@ public function connect(bool $persistent = false)
127127
*/
128128
public function reconnect()
129129
{
130-
$this->close();
131-
$this->initialize();
130+
if ($this->ping() === false) {
131+
$this->close();
132+
$this->initialize();
133+
}
132134
}
133135

134136
/**

user_guide_src/source/changelogs/v4.7.0.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,8 @@ Testing
289289
Database
290290
========
291291

292-
- **BaseConnection:** Added ``ping()`` method to check if the database connection is still alive. Postgre uses native ``pg_ping()``, other drivers use ``SELECT 1``.
292+
- **BaseConnection:** Added ``ping()`` method to check if the database connection is still alive. OCI8 uses ``SELECT 1 FROM DUAL``, other drivers use ``SELECT 1``.
293+
- **BaseConnection:** The ``reconnect()`` method now uses ``ping()`` to check if the connection is alive before reconnecting. Previously, some drivers would always close and reconnect unconditionally, and OCI8's ``reconnect()`` did nothing.
293294
- **Exception Logging:** All DB drivers now log database exceptions uniformly. Previously, each driver had its own log format.
294295

295296
Query Builder

user_guide_src/source/database/connecting.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,12 @@ Reconnecting / Keeping the Connection Alive
9494

9595
If the database server's idle timeout is exceeded while you're doing
9696
some heavy PHP lifting (processing an image, for instance), you should
97-
consider pinging the server by using the ``reconnect()`` method before
98-
sending further queries, which can gracefully keep the connection alive
99-
or re-establish it.
97+
consider calling the ``reconnect()`` method before sending further queries,
98+
which can gracefully keep the connection alive or re-establish it.
10099

101-
.. important:: If you are using MySQLi database driver, the ``reconnect()`` method
102-
does not ping the server but it closes the connection then connects again.
100+
The ``reconnect()`` method pings the server to check if the connection is still
101+
alive. If the connection has been lost, it will close and re-establish the
102+
connection.
103103

104104
.. literalinclude:: connecting/007.php
105105
:lines: 2-

0 commit comments

Comments
 (0)