Skip to content

Commit cf16d3c

Browse files
committed
Prevent stale connections
1 parent f5af31b commit cf16d3c

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

packages/database/src/Connection/ConnectionInitializer.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,37 @@
1111

1212
final class ConnectionInitializer implements Initializer
1313
{
14-
private static ?Connection $connection = null;
14+
/** @var Connection[] */
15+
private static array $connections = [];
1516

1617
#[Singleton]
1718
public function initialize(Container $container): Connection
1819
{
1920
$config = $container->get(DatabaseConfig::class);
21+
$connectionKey = $this->getConnectionKey($config);
2022

2123
$connection = $config->usePersistentConnection
22-
? self::$connection
24+
? self::$connections[$connectionKey] ?? null
2325
: null;
2426

2527
if (! $connection instanceof Connection) {
2628
$connection = new PDOConnection($config);
2729
$connection->connect();
28-
self::$connection = $connection;
30+
self::$connections[$connectionKey] = $connection;
2931
} elseif ($connection->ping() === false) {
3032
$connection->reconnect();
3133
}
3234

3335
return $connection;
3436
}
37+
38+
private function getConnectionKey(DatabaseConfig $config): string
39+
{
40+
return hash('xxh128', serialize([
41+
$config->dsn,
42+
$config->username,
43+
$config->options,
44+
$config->password,
45+
]));
46+
}
3547
}

0 commit comments

Comments
 (0)