Skip to content

Commit c46af2c

Browse files
committed
✨ Improve MySQL reconnect logic and remove stale connection caching 🔧
- Remove connectionMaster singleton that could return stale database connections - Refactor reconnect() to properly reset counter on success and only increment on failure - Remove implicit reconnect on forked processes (php-resque) to avoid unintended reconnections - Add explicit error handling for reconnection failures by catching and re-throwing exceptions - Close cursor after SELECT 1 health check to free up resources
1 parent ef2c4ba commit c46af2c

2 files changed

Lines changed: 8 additions & 17 deletions

File tree

lhc_web/ezcomponents/Database/src/handlers/mysql.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,22 +124,18 @@ public function __construct( $dbParams )
124124
public function reconnect()
125125
{
126126
try {
127-
@$this->query('SELECT 1');
127+
$this->query('SELECT 1')->closeCursor();
128128
} catch (Exception $e) {
129129
if ($e->errorInfo[1] == 2006 && $this->reconnectedCounter < 5) {
130-
$this->reconnectedCounter++;
131-
$this->reconnectClean();
130+
try {
131+
$this->reconnectClean();
132+
$this->reconnectedCounter = 0;
133+
} catch (Exception $eReconnect) {
134+
$this->reconnectedCounter++;
135+
throw $eReconnect;
136+
}
132137
}
133138
}
134-
135-
// After a fork (php-resque workers), PDO may report an active
136-
// transaction on the inherited connection even though ezc's
137-
// $transactionNestingLevel was reset to 0 in the child. Force
138-
// a fresh connection to clear the stale PDO state.
139-
if ($this->inTransaction()) {
140-
$this->reconnectedCounter++;
141-
$this->reconnectClean();
142-
}
143139
}
144140

145141
private function reconnectClean()

lhc_web/lib/core/lhcore/lhdb.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
class erLhcoreClassLazyDatabaseConfiguration implements ezcBaseConfigurationInitializer
44
{
5-
private static $connectionMaster;
65
public static $connectionTime = null;
76
public static $connectionStartTime = null;
87

@@ -33,7 +32,6 @@ public static function configureObject( $instance )
3332
return $db;
3433
} else {
3534
// Perhaps connection is already done with master?
36-
if (isset(self::$connectionMaster)) return self::$connectionMaster;
3735
try {
3836
$db = ezcDbFactory::create( "mysql://{$cfg->getSetting( 'db', 'user' )}:{$cfg->getSetting( 'db', 'password' )}@{$cfg->getSetting( 'db', 'host' )}:{$cfg->getSetting( 'db', 'port' )}/{$cfg->getSetting( 'db', 'database' )}" );
3937
$db->query("SET NAMES 'utf8mb4' COLLATE 'utf8mb4_unicode_ci'");
@@ -44,7 +42,6 @@ public static function configureObject( $instance )
4442
} catch (Exception $e) {
4543
}
4644
}
47-
self::$connectionMaster = $db;
4845
return $db;
4946
} catch (Exception $e) {
5047
error_log($e);
@@ -67,7 +64,6 @@ public static function configureObject( $instance )
6764
case false: // Default instance
6865
{
6966
try {
70-
if (isset(self::$connectionMaster)) return self::$connectionMaster; // If we do not user slaves and slave request already got connection
7167
self::$connectionStartTime = microtime(true);
7268
$db = ezcDbFactory::create( "mysql://{$cfg->getSetting( 'db', 'user' )}:{$cfg->getSetting( 'db', 'password' )}@{$cfg->getSetting( 'db', 'host' )}:{$cfg->getSetting( 'db', 'port' )}/{$cfg->getSetting( 'db', 'database' )}" );
7369
$db->query("SET NAMES 'utf8mb4' COLLATE 'utf8mb4_unicode_ci'");
@@ -80,7 +76,6 @@ public static function configureObject( $instance )
8076
}
8177
}
8278
self::$connectionTime = microtime(true) - self::$connectionStartTime;
83-
self::$connectionMaster = $db;
8479
return $db;
8580
} catch (Exception $e) {
8681
// Are we installed?

0 commit comments

Comments
 (0)