Event fired on database connection failure.

@param FailedConnectionEvent $args

@throws DatabaseConnectionException
330898
public function failConnect(FailedConnectionEvent $args)
    {
        $e = $args->getException();
        $this->logger->debug($e->getMessage(), ['event' => 'exception', 'exception' => $e]);

        throw new DatabaseConnectionException($args->getDriver()->getName(), $e->getMessage(), $e);
    }
486929
private function tryConnect(): void
    {
        $config = $this->getConfig();

        $pdoConfig = [
          PDO::ATTR_EMULATE_PREPARES => false,
          PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
          PDO::ATTR_PERSISTENT => $config['persistent'] ?? true,
          PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
        ];

        if (isset($config['adapter_options'])) {
            foreach ($config['adapter_options'] as $key => $value) {
                $pdoConfig[$key] = $value;
            }
        }

        if (!isset($config['socket'])) {
            // not a socket
            $port = $config['port'] ?? 5432;

            $this->pdo = new PDO("pgsql:host={$config['host']};port={$port};dbname={$config['database']}", $config['username'], $config['password'], $pdoConfig);
        } else {
            // the case with unix_socket
            $this->pdo = new PDO("pgsql:dbname={$config['database']}", $config['username'], $config['password'], $pdoConfig);
        }

        if (isset($config['schema'])) {
            $schemaSql = 'SET search_path TO ' . $config['schema'];
            $this->pdo->exec($schemaSql);
            Log::sql($schemaSql);
        }
    }
878038
public function checkConnectResult($sock, $flag, $arg)
    {
        $this->connectComplete();
        $this->removeEventLooper();
        
        $sock = socket_import_stream($this->socket);
        $code = socket_get_option($sock, SOL_SOCKET, SO_ERROR);
        $errstr = socket_strerror($code);
        
        if($code) {
            Yii::warning($this->target ." connect failed($code), $errstr", 'beyod');
            $this->trigger(Server::ON_CONNECT_FAILED, new ErrorEvent(['code'=>$code, 'errstr'=>$errstr]));
            return ;
        }
        
        $this->status = static::STATUS_ESTABLISHED;
        $this->id = static::generateId();
        
        static::$count++;
        
        static::$connections[$this->id] = $this;
        
        $this->peer = stream_socket_get_name($this->socket, true);
        $this->local = stream_socket_get_name($this->socket, false);
        
        Yii::debug('connected to '.$this->target.', '.$this, 'beyod');
        
        $this->trigger(Server::ON_CONNECT, new IOEvent());
        
        Yii::$app->eventLooper->addFdEvent($this->socket, EventLooper::EV_READ, [$this, 'readBuffer']);
        
        $this->afterConnected();
    }
525991
public static function fail($format, $args = null, $_ = null) {
        throw new EnsureException(vsprintf($format, array_slice(func_get_args(), 1)));
    }
79424
function _pconnect($argDSN, $argUsername, $argPassword, $argDatabasename)
	{
	global $php_errormsg;

		if (!function_exists('odbc_connect')) return null;

		if (isset($php_errormsg)) $php_errormsg = '';
		$this->_errorMsg = isset($php_errormsg) ? $php_errormsg : '';
		if ($this->debug && $argDatabasename) {
			ADOConnection::outp("For odbc PConnect(), $argDatabasename is not used. Place dsn in 1st parameter.");
		}
	//	print "dsn=$argDSN u=$argUsername p=$argPassword<br>"; flush();
		if ($this->curmode === false) $this->_connectionID = odbc_connect($argDSN,$argUsername,$argPassword);
		else $this->_connectionID = odbc_pconnect($argDSN,$argUsername,$argPassword,$this->curmode);

		$this->_errorMsg = isset($php_errormsg) ? $php_errormsg : '';
		if ($this->_connectionID && $this->autoRollback) @odbc_rollback($this->_connectionID);
		if (isset($this->connectStmt)) $this->Execute($this->connectStmt);

		return $this->_connectionID != false;
	}
207251
final public function connect()
    {
        /* If an established connection is available, then there's
         * no need to connect and select the database.
         *
         * Depending on the database driver, conn_id can be either
         * boolean TRUE, a resource or an object.
         */
        if ($this->handle) {
            return;
        }

        //--------------------------------------------------------------------

        $this->connectTimeStart = microtime(true);

        // Connect to the database and set the connection ID
        $this->platformConnectHandler($this->config);

        // No connection resource? Check if there is a failover else throw an error
        if ( ! $this->handle) {
            // Check if there is a failover set
            if ( ! empty($this->config[ 'failover' ]) && is_array($this->config[ 'failover' ])) {
                // Go over all the failovers
                foreach ($this->config[ 'failover' ] as $failover) {

                    // Try to connect
                    $this->platformConnectHandler($failover = new Config($failover));

                    // If a connection is made break the foreach loop
                    if ($this->handle) {
                        $this->config = $failover;
                        break;
                    }
                }
            }

            // We still don't have a connection?
            if ( ! $this->handle) {
                throw new RuntimeException('DB_E_UNABLE_TO_CONNECT', 0, [$this->platform]);
            }
        }

        $this->connectTimeDuration = microtime(true) - $this->connectTimeStart;
    }
1348314
function _connect($argHostname, $argUsername, $argPassword, $argDatabasename)
	{
		if (!function_exists('sqlsrv_connect')) return null;
		$connectionInfo = $this->connectionInfo;
		$connectionInfo["Database"]=$argDatabasename;
		$connectionInfo["UID"]=$argUsername;
		$connectionInfo["PWD"]=$argPassword;
		
		foreach ($this->connectionParameters as $parameter=>$value)
		    $connectionInfo[$parameter] = $value;
		
		if ($this->debug) ADOConnection::outp("<hr>connecting... hostname: $argHostname params: ".var_export($connectionInfo,true));
		//if ($this->debug) ADOConnection::outp("<hr>_connectionID before: ".serialize($this->_connectionID));
		if(!($this->_connectionID = sqlsrv_connect($argHostname,$connectionInfo))) {
			if ($this->debug) ADOConnection::outp( "<hr><b>errors</b>: ".print_r( sqlsrv_errors(), true));
			return false;
		}
		//if ($this->debug) ADOConnection::outp(" _connectionID after: ".serialize($this->_connectionID));
		//if ($this->debug) ADOConnection::outp("<hr>defined functions: <pre>".var_export(get_defined_functions(),true)."</pre>");
		return true;
	}
1154454
public void fireConnectionErrorOccured(SQLException ex) {
    ConnectionEvent event = new ConnectionEvent(this, ex);
    for (ConnectionEventListener listener : connectionEventListeners) {
      listener.connectionErrorOccurred(event);
    }
  }
83687
private function send(string $methodName, ...$args): Promise
    {
        if (! $this->handle) {
            throw new FailureException('Not connected');
        }

        if ($this->busy) {
            return call(function () use ($methodName, $args) {
                while ($this->busy) {
                    yield $this->busy->promise();
                }

                return yield ([$this->handle, $methodName])(...$args);
            });
        }

        return ([$this->handle, $methodName])(...$args);
    }
1173020
function _connect($argDSN, $argUsername, $argPassword, $argDatabasename)
  {
          global $php_errormsg;

    if (!function_exists('ads_connect')) return null;

    if ($this->debug && $argDatabasename && $this->databaseType != 'vfp') {
      ADOConnection::outp("For Advantage Connect(), $argDatabasename is not used. Place dsn in 1st parameter.");
    }
    if (isset($php_errormsg)) $php_errormsg = '';
    if ($this->curmode === false) $this->_connectionID = ads_connect($argDSN,$argUsername,$argPassword);
    else $this->_connectionID = ads_connect($argDSN,$argUsername,$argPassword,$this->curmode);
    $this->_errorMsg = isset($php_errormsg) ? $php_errormsg : '';
    if (isset($this->connectStmt)) $this->Execute($this->connectStmt);

    return $this->_connectionID != false;
  }
706594
function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
	{
		//return null;//not implemented. NOTE: Persistent connections have no effect if PHP is used as a CGI program. (FastCGI!)
		return $this->_connect($argHostname, $argUsername, $argPassword, $argDatabasename);
	}
415020
function _pconnect($argDSN, $argUsername, $argPassword, $argDatabasename)
	{
		return $this->_connect($argDSN, $argUsername, $argPassword, $argDatabasename, true);
	}
831668
private function tryConnect(): void
    {
        $config = $this->getConfig();

        $pdoConfig = [
          PDO::ATTR_EMULATE_PREPARES => false,
          PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
          PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false,
          PDO::ATTR_PERSISTENT => $config['persistent'] ?? true,
          PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
        ];

        if (isset($config['adapter_options'])) {
            foreach ($config['adapter_options'] as $key => $value) {
                $pdoConfig[$key] = $value;
            }
        }

        $charset = $config['charset'] ?? 'utf8';

        if (!isset($config['socket'])) {
            // not a socket
            $port = $config['port'] ?? 3306;

            $this->pdo = new PDO("mysql:host={$config['host']};port={$port};dbname={$config['database']};charset={$charset}", $config['username'], $config['password'], $pdoConfig);
        } else {
            // the case with unix_socket
            $this->pdo = new PDO("mysql:unix_socket={$config['socket']};dbname={$config['database']};charset={$charset}", $config['username'], $config['password'], $pdoConfig);
        }
    }
718111
function _connect($argDSN, $argUsername, $argPassword, $argDatabasename)
	{
	global $php_errormsg;

		if (!function_exists('odbc_connect')) return null;

		if (!empty($argDatabasename) && stristr($argDSN, 'Database=') === false) {
			$argDSN = trim($argDSN);
			$endDSN = substr($argDSN, strlen($argDSN) - 1);
			if ($endDSN != ';') $argDSN .= ';';
			$argDSN .= 'Database='.$argDatabasename;
		}

		if (isset($php_errormsg)) $php_errormsg = '';
		if ($this->curmode === false) $this->_connectionID = odbc_connect($argDSN,$argUsername,$argPassword);
		else $this->_connectionID = odbc_connect($argDSN,$argUsername,$argPassword,$this->curmode);
		$this->_errorMsg = isset($php_errormsg) ? $php_errormsg : '';
		if (isset($this->connectStmt)) $this->Execute($this->connectStmt);

		return $this->_connectionID != false;
	}
35884
public function onIOError(Connection $conn, \Exception $ex)
    {
        try {
            $conn->tryClose();
        } catch (\Throwable $t) {
            sys_echo("nsq({$conn->getAddr()}) onIOError exception: {$t->getMessage()}");
        } catch (\Exception $ex) {
            sys_echo("nsq({$conn->getAddr()}) onIOError exception: {$ex->getMessage()}");
        }
    }
909819
public static function fireEvent($event, $args = array(), $resultAsString = false)
    {
        $results = array();
        
        if (isset(self::$events[$event])) {
            $keys = array_keys(self::$events[$event]); 
            sort($keys);
            foreach ($keys as $key) {
                $callback = self::$events[$event][$key];
                $results[$key] = call_user_func_array($callback, $args);
            }
        }
        
        if ($resultAsString) {
            return implode('', $results);
        }
        return $results;
    }
1287203
public function emit($event, $args=null)
    {
        $listers_of_event = isset($this->listeners[$event]) ? $this->listeners[$event] : null;
        if ($listers_of_event){
            foreach ($listers_of_event as $listener){
                call_user_func($listener,$args,$event);
            }
        }
    }