- if ( $domain->getDatabase() === null ) {
- // The database domain does not specify a DB name and some database systems require a
- // valid DB specified on connection. The $server configuration array contains a default
- // DB name to use for connections in such cases.
- if ( $server['type'] === 'mysql' ) {
- // For MySQL, DATABASE and SCHEMA are synonyms, connections need not specify a DB,
- // and the DB name in $server might not exist due to legacy reasons (the default
- // domain used to ignore the local LB domain, even when mismatched).
- $server['dbname'] = null;
- }
- } else {
- $server['dbname'] = $domain->getDatabase();
- }
-
- if ( $domain->getSchema() !== null ) {
- $server['schema'] = $domain->getSchema();
- }
-
- // It is always possible to connect with any prefix, even the empty string
- $server['tablePrefix'] = $domain->getTablePrefix();
-
- // Let the handle know what the cluster master is (e.g. "db1052")
- $masterName = $this->getServerName( $this->getWriterIndex() );
- $server['clusterMasterHost'] = $masterName;
-
- $server['srvCache'] = $this->srvCache;
- // Set loggers and profilers
- $server['connLogger'] = $this->connLogger;
- $server['queryLogger'] = $this->queryLogger;
- $server['errorLogger'] = $this->errorLogger;
- $server['deprecationLogger'] = $this->deprecationLogger;
- $server['profiler'] = $this->profiler;
- $server['trxProfiler'] = $this->trxProfiler;
- // Use the same agent and PHP mode for all DB handles
- $server['cliMode'] = $this->cliMode;
- $server['agent'] = $this->agent;
- // Use DBO_DEFAULT flags by default for LoadBalancer managed databases. Assume that the
- // application calls LoadBalancer::commitMasterChanges() before the PHP script completes.
- $server['flags'] = $server['flags'] ?? IDatabase::DBO_DEFAULT;
-
- // Create a live connection object
+ $server = $this->getServerInfoStrict( $i );
+ $server = array_merge( $server, [
+ // Use the database specified in $domain (null means "none or entrypoint DB");
+ // fallback to the $server default if the RDBMs is an embedded library using a file
+ // on disk since there would be nothing to access to without a DB/file name.
+ 'dbname' => $this->getServerAttributes( $i )[Database::ATTR_DB_IS_FILE]
+ ? ( $domain->getDatabase() ?? $server['dbname'] ?? null )
+ : $domain->getDatabase(),
+ // Override the $server default schema with that of $domain if specified
+ 'schema' => $domain->getSchema() ?? $server['schema'] ?? null,
+ // Use the table prefix specified in $domain
+ 'tablePrefix' => $domain->getTablePrefix(),
+ // Participate in transaction rounds if $server does not specify otherwise
+ 'flags' => $server['flags'] ?? IDatabase::DBO_DEFAULT,
+ // Inject the PHP execution mode and the agent string
+ 'cliMode' => $this->cliMode,
+ 'agent' => $this->agent,
+ // Inject object and callback dependencies
+ 'srvCache' => $this->srvCache,
+ 'connLogger' => $this->connLogger,
+ 'queryLogger' => $this->queryLogger,
+ 'errorLogger' => $this->errorLogger,
+ 'deprecationLogger' => $this->deprecationLogger,
+ 'profiler' => $this->profiler,
+ 'trxProfiler' => $this->trxProfiler
+ ] );
+
+ $lbInfo = array_merge( $lbInfo, [
+ 'ownerId' => $this->id,
+ 'serverIndex' => $i,
+ 'master' => ( $i === $this->getWriterIndex() ),
+ 'replica' => ( $i !== $this->getWriterIndex() ),
+ // Name of the master server of the relevant DB cluster (e.g. "db1052")
+ 'clusterMasterHost' => $this->getServerName( $this->getWriterIndex() )
+ ] );
+
+ $conn = Database::factory( $server['type'], $server, Database::NEW_UNCONNECTED );