function ping() { if (!$this->conn) { return $this->connect(); } return pg_ping($this->conn); }
public function is_connected() { return $this->connection_handle && pg_ping($this->connection_handle); }
/** * Reconnect. * * Keep / reestablish the db connection if no queries have been * sent for a length of time exceeding the server's idle timeout * * @return void */ public function reconnect() { if (pg_ping($this->conn_id) === false) { $this->conn_id = false; } }
/** * 检查连接是否可用 * * 防止因长时间不链接而导致连接丢失的问题 server has gone away * * @throws Exception */ protected function _check_connect() { # 5秒检测1次 static $error_num = 0; try { $connection_id = $this->connection_id(); $connection = Database_Driver_Postgre::$_connection_instance[$connection_id]; if ($connection) { $ping_status = pg_ping($connection); } else { $ping_status = false; } } catch (Exception $e) { $error_num++; $ping_status = false; } if (!$ping_status) { if ($error_num < 5) { $this->close_connect(); # 等待3毫秒 usleep(3000); # 再次尝试连接 $this->connect(); $error_num = 0; } else { throw new Exception('connect postgre server error'); } } }
/** * Checks, if the connection to the server is still alive * * @return bool */ public function ping() { // Checks, if the connection to the server is still alive if (!$this->objServer) { throw new Exception('No SQL resource available. Could not execute ping.'); } return \pg_ping($this->objServer); }
/** * This function tries to open to the DB * * @param string $pDbDriver * @param array $arrayServerData * * @return void */ public function tryOpenDataBase($pDbDriver, array $arrayServerData = array()) { if ($this->errno != 0) { return 0; } set_time_limit(0); $stat = new Stat(); $flagTns = isset($arrayServerData["connectionType"]) && $arrayServerData["connectionType"] == "TNS" ? 1 : 0; if (isset($this->db_user) && (isset($this->db_passwd) || $this->db_passwd == "") && (isset($this->db_sourcename) || $flagTns == 1)) { switch ($pDbDriver) { case 'mysql': $link = @mysql_connect($this->ip . ($this->db_port != '' && $this->db_port != 0 ? ':' . $this->db_port : ''), $this->db_user, $this->db_passwd); $db = @mysql_select_db($this->db_sourcename); if ($link) { if ($db) { $result = @mysql_query("show tables;"); if ($result) { $stat->status = 'SUCCESS'; $this->errstr = ""; $this->errno = 0; @mysql_free_result($result); } else { $this->error = "the user {$this->db_user} doesn't have privileges to run queries!"; $this->errstr = "NET::MYSQL->Test query failed"; $this->errno = 10100; } } else { $this->error = "The {$this->db_sourcename} data base does'n exist!"; $this->errstr = "NET::MYSQL->Select data base failed"; $this->errno = 10011; } } else { $this->error = "MySql connection refused!"; $this->errstr = "NET::MYSQL->The connection was refused"; $this->errno = 10001; } break; case 'pgsql': $this->db_port = $this->db_port == "" || $this->db_port == 0 ? "5432" : $this->db_port; $link = @pg_connect("host='{$this->ip}' port='{$this->db_port}' user='******' password='******' dbname='{$this->db_sourcename}'"); if ($link) { if (@pg_ping($link)) { $stat->status = 'SUCCESS'; $this->errstr = ""; $this->errno = 0; } else { $this->error = "PostgreSql Connection to {$this->ip} is unreachable!"; $this->errstr = "NET::POSTGRES->Lost Connection"; $this->errno = 20010; } } else { $this->error = "PostgrSql connection refused"; $this->errstr = "NET::POSTGRES->The connection was refused"; $this->errno = 20001; } break; case 'mssql': // $str_port = (($this->db_port == "") || ($this->db_port == 0) || ($this->db_port == 1433)) ? "" : ":".$this->db_port; // $link = @mssql_connect($this->ip . $str_port, $this->db_user, $this->db_passwd); if ($this->db_instance != "") { $str_port = ""; $link = @mssql_connect($this->ip . "\\" . $this->db_instance, $this->db_user, $this->db_passwd); } else { $str_port = $this->db_port == "" || $this->db_port == 0 || $this->db_port == 1433 ? "" : ":" . $this->db_port; $link = @mssql_connect($this->ip . $str_port, $this->db_user, $this->db_passwd); } if ($link) { $db = @mssql_select_db($this->db_sourcename, $link); if ($db) { $stat->status = 'SUCCESS'; $this->errstr = ""; $this->errno = 0; } else { $this->error = "The {$this->db_sourcename} data base does'n exist!"; $this->errstr = "NET::MSSQL->Select data base failed"; $this->errno = 30010; } } else { $this->error = "MS-SQL Server connection refused!"; $this->errstr = "NET::MSSQL->The connection was refused"; $this->errno = 30001; } break; case 'oracle': if ($flagTns == 0) { $this->db_port = $this->db_port == "" || $this->db_port == 0 ? "1521" : $this->db_port; $cnn = @oci_connect($this->db_user, $this->db_passwd, "(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP) (HOST={$this->ip}) (PORT={$this->db_port}) )) (CONNECT_DATA=(SERVICE_NAME={$this->db_sourcename})))"); } else { $cnn = @oci_connect($this->db_user, $this->db_passwd, $arrayServerData["tns"]); } if ($cnn) { $stid = @oci_parse($cnn, 'select AUTHENTICATION_TYPE from v$session_connect_info'); $result = @oci_execute($stid, OCI_DEFAULT); if ($result) { $stat->status = 'SUCCESS'; $this->errstr = ""; $this->errno = 0; @oci_close($cnn); } else { $this->error = "the user {$this->db_user} doesn't have privileges to run queries!"; $this->errstr = "NET::ORACLE->Couldn't execute any query on this server!"; $this->errno = 40010; } } else { $this->error = "Oracle connection refused!"; $this->errstr = "NET::ORACLE->The connection was refused"; $this->errno = 40001; } break; case 'informix': break; case 'sqlite': break; } } else { throw new Exception("CLASS::NET::ERROR: No connections param."); } return $stat; }
public function ping() { return pg_ping($this->connection); }
/** * Check the connection to PgSQL if Active and Not Busy * If not connected will connect * PRIVATE * * @param RESOURCE $y_connection :: The Connection to PgSQL Server * @param STRING $y_description :: The Description of Where it is Checked (for having a clue where it fails) * @return Connection Resource * */ private static function check_connection($y_connection, $y_description) { //-- global $configs; //-- if ($y_connection === 'DEFAULT') { // just for the default connection !!! //-- if (!defined('SMART_FRAMEWORK_DB_LINK_PostgreSQL')) { // PostgreSQL default connection is exported as constant to avoid re-connection which can break transactions //-- if (!is_array($configs['pgsql'])) { self::error('', 'CHECK-DEFAULT-PGSQL-CONFIGS', 'The Default PostgreSQL Configs not detected !', 'The configs[pgsql] is not an array !', $y_description); return null; } //end if if ((string) $configs['pgsql']['server-host'] == '' or (string) $configs['pgsql']['server-port'] == '' or (string) $configs['pgsql']['dbname'] == '' or (string) $configs['pgsql']['username'] == '') { self::error('', 'CHECK-DEFAULT-PGSQL-CONFIGS', 'The Default PostgreSQL Configs are not complete !', 'Some of the configs[pgsql] parameters are missing !', $y_description); return null; } //end if //-- {{{SYNC-CONNECTIONS-IDS}}} $the_conn_key = (string) $configs['pgsql']['server-host'] . ':' . $configs['pgsql']['server-port'] . '@' . $configs['pgsql']['dbname'] . '#' . $configs['pgsql']['username'] . '>' . trim(strtoupper(str_replace(' ', '', (string) $configs['pgsql']['transact']))) . '.'; if (array_key_exists((string) $the_conn_key, (array) SmartFrameworkRegistry::$Connections['pgsql'])) { // if the connection was made before using the SmartPgsqlExtDb //-- $y_connection = SmartFrameworkRegistry::$Connections['pgsql'][(string) $the_conn_key]; //-- define('SMART_FRAMEWORK_DB_LINK_PostgreSQL', $y_connection); //-- if ((string) SMART_FRAMEWORK_DEBUG_MODE == 'yes') { SmartFrameworkRegistry::setDebugMsg('db', 'pgsql|log', ['type' => 'open-close', 'data' => 'Re-Using Connection to PgSQL Server as DEFAULT: ' . $the_conn_key, 'connection' => (string) $y_connection]); } //end if //-- } else { //-- $y_connection = self::server_connect((string) $configs['pgsql']['server-host'], (int) $configs['pgsql']['server-port'], (string) $configs['pgsql']['dbname'], (string) $configs['pgsql']['username'], (string) $configs['pgsql']['password'], (int) $configs['pgsql']['timeout'], (string) $configs['pgsql']['transact'], (double) $configs['pgsql']['slowtime'], (string) $configs['pgsql']['type']); //-- define('SMART_FRAMEWORK_DB_LINK_PostgreSQL', $y_connection); //-- if (is_resource($y_connection)) { //-- define('SMART_FRAMEWORK_DB_VERSION_PostgreSQL', self::check_server_version($y_connection, true)); // re-validate //-- } //end if //-- } //end if else //-- } else { //-- re-use the default connection $y_connection = SMART_FRAMEWORK_DB_LINK_PostgreSQL; //-- } //end if //-- } //end if //-- if (!is_resource($y_connection)) { // if no connection //-- self::error($y_connection, 'CHECK-CONNECTION', 'Connection is BROKEN !', 'Connection-ID: ' . $y_connection, $y_description); return null; //-- } //end if //-- if (@pg_connection_status($y_connection) != PGSQL_CONNECTION_OK) { //-- $re_connect = @pg_ping($y_connection); //-- if (!$re_connect) { self::error($y_connection, 'CHECK-CONNECTION', 'Connection LOST !', 'Connection-ID: ' . $y_connection, $y_description); return null; } //end if //-- } //end if else //-- return $y_connection; //-- }
/** * Vérifie l'état de la connexion courante et effectue si besoin une reconnexion * * @access public * @return boolean */ function ping() { return pg_ping($this->link); }
/** * transmit a ping against the database * * @return bool true | false */ public function ping() { return pg_ping($this->dbh) ? true : false; }
/** * @return bool */ protected function hasDbConnection() { if ($this->pg_conn) { return pg_ping($this->pg_conn); } else { return pg_ping(); } }
public function getProtocolInfo() { return pg_ping($this->link); }
<?php // optional functions include 'config.inc'; $db = pg_connect($conn_str); var_dump(pg_ping($db));
/** * Determines if the connection to the server is active. * * @return boolean * * @since 12.1 */ public function connected() { $this->connect(); if (is_resource($this->connection)) { return pg_ping($this->connection); } return false; }
function isValid() { return @pg_ping($this->getConnectionId()); }
/** * Reconnect * * Keep / reestablish the db connection if no queries have been * sent for a length of time exceeding the server's idle timeout * * @return void */ public function reconnect() { if (pg_ping($this->conn_id) === FALSE) { $this->conn_id = FALSE; } }
public function Open() { if (!$this->connected) { try { // Build params (omit hostname and password to connect using Unix sockets) $params = ''; if (!empty($this->connect['server']) && $this->connect['server'] != 'localhost') { $params = "host='" . $this->connect['server'] . "' "; } if (!empty($this->connect['port']) && $this->connect['port'] != self::DEFAULT_PORT) { $params = "port='" . $this->connect['port'] . "' "; } $params .= (!empty($this->connect['database']) ? "dbname='" . $this->connect['database'] . "' " : '') . (!empty($this->connect['username']) ? "user='******'username'] . "' " : '') . (!empty($this->connect['password']) ? "password='******'password'] . "' " : ''); // Connect if ($this->connect['persistent']) { $this->connection = pg_pconnect($params); } else { $this->connection = pg_connect($params); } // Make sure persistent connection is still alive if (!pg_ping($this->connection)) { throw new DatabaseException('Lost connection to database: unable to reconnect'); } // Enable Unicode pg_set_client_encoding($this->connection, 'UNICODE'); // Get prepared statements for this (persistent) connection $result = pg_query($this->connection, 'SELECT * FROM "pg_prepared_statements"'); if ($result === false) { throw new SQLException(pg_last_error($this->connection), 0, (string) $query); } while ($row = pg_fetch_assoc($result)) { $this->statements[$row['name']] = $row; } unset($this->connect['password']); $this->connected = true; } catch (Exception $e) { throw new DatabaseException($e->getMessage()); } } }