- public function openConnection( $i, $domain = false, $flags = 0 ) {
- if ( $this->localDomain->equals( $domain ) || $domain === $this->localDomainIdAlias ) {
- $domain = false; // local connection requested
- }
-
- if ( !$this->connectionAttempted && $this->chronologyCallback ) {
- $this->connLogger->debug( __METHOD__ . ': calling initLB() before first connection.' );
- // Load any "waitFor" positions before connecting so that doWait() is triggered
- $this->connectionAttempted = true;
- ( $this->chronologyCallback )( $this );
- }
-
- // Check if an auto-commit connection is being requested. If so, it will not reuse the
- // main set of DB connections but rather its own pool since:
- // a) those are usually set to implicitly use transaction rounds via DBO_TRX
- // b) those must support the use of explicit transaction rounds via beginMasterChanges()
- $autoCommit = ( ( $flags & self::CONN_TRX_AUTOCOMMIT ) == self::CONN_TRX_AUTOCOMMIT );
-
- if ( $domain !== false ) {
- // Connection is to a foreign domain
- $conn = $this->openForeignConnection( $i, $domain, $flags );
- } else {
- // Connection is to the local domain
- $conn = $this->openLocalConnection( $i, $flags );
- }
-
- if ( $conn instanceof IDatabase && !$conn->isOpen() ) {
- // Connection was made but later unrecoverably lost for some reason.
- // Do not return a handle that will just throw exceptions on use,
- // but let the calling code (e.g. getReaderIndex) try another server.
- // See DatabaseMyslBase::ping() for how this can happen.
- $this->errorConnection = $conn;
- $conn = false;
- }
-
- if ( $autoCommit && $conn instanceof IDatabase ) {
- if ( $conn->trxLevel() ) { // sanity
- throw new DBUnexpectedError(
- $conn,
- __METHOD__ . ': CONN_TRX_AUTOCOMMIT handle has a transaction.'
- );
- }
-
- $conn->clearFlag( $conn::DBO_TRX ); // auto-commit mode
- }