File tree Expand file tree Collapse file tree
packages/database/src/Connection Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1111
1212final 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}
You can’t perform that action at this time.
0 commit comments