PHP
·
发表于 5年以前
·
阅读量:8296
mysql_ping() ¼i²eµ½þÎñÆ÷µÄÁ¬½ÓÊÇñÕý³£¡£Èç¹uµ½þÎñÆ÷µÄÁ¬½Ó¿ÉÓã¬Ôoµ»Øtrue£¬ñÔoµ»Øfalse¡£
µ«PDO²»Ö§³Ö**mysql_ping()**½¨£¬Òo´ËÐeÒª×Ô¼º±aд´uÂeÄ£Äamysql_ping()½¨£¬¼i²eÁ¬½ÓÊÇ*ñ¿ÉÓá£
´uÂeÈçÏ£º
<?php
/**
* ¼i²eÁ¬½ÓÊÇ*ñ¿ÉÓÃ
* @param Link $dbconn Êý¾Ý¿aÁ¬½Ó
* @return Boolean
*/
function pdo_ping($dbconn){
try{
$dbconn->getAttribute(PDO::ATTR_SERVER_INFO);
} catch (PDOException $e) {
if(strpos($e->getMessage(), 'MySQL server has gone away')!==false){
return false;
}
}
return true;
}
?>
´uÂeÑÝʾ£º
1¡¢´´½¨²aÊÔÊý¾Ý±i
CREATE TABLE `user` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2¡¢²aÈe²aÊÔÊý¾Ý
insert into user(name) values('fdipzone'),('xfdipzone'),('terry');
mysql> select * from user;
+----+-----------+
| id | name |
+----+-----------+
| 1 | fdipzone |
| 2 | xfdipzone |
| 3 | terry |
+----+-----------+
3¡¢ÑÝʾÎļþ
db.php
<?php
// Êý¾Ý¿a²Ù×÷Àa
class DB{
// ±£´aeÊý¾Ý¿aÁ¬½Ó
private static $_instance = null;
// Á¬½ÓÊý¾Ý¿a
public static function get_conn($config){
if(isset(self::$_instance) && !empty(self::$_instance)){
return self::$_instance;
}
$dbhost = $config['host'];
$dbname = $config['dbname'];
$dbuser = $config['user'];
$dbpasswd = $config['password'];
$pconnect = $config['pconnect'];
$charset = $config['charset'];
$dsn = "mysql:host=$dbhost;dbname=$dbname;";
try {
$h_param = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
);
if ($charset != '') {
$h_param[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET NAMES ' . $charset; //ÔOÖÃĬÕJ¾Ž´a
}
if ($pconnect) {
$h_param[PDO::ATTR_PERSISTENT] = true;
}
$conn = new PDO($dsn, $dbuser, $dbpasswd, $h_param);
} catch (PDOException $e) {
throw new ErrorException('Unable to connect to db server. Error:' . $e->getMessage(), 31);
}
self::$_instance = $conn;
return $conn;
}
// Ö´Ðвeѯ
public static function query($dbconn, $sqlstr, $condparam){
$sth = $dbconn->prepare($sqlstr);
try{
$sth->execute($condparam);
} catch (PDOException $e) {
echo $e->getMessage().PHP_EOL;
}
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
// ÖØÖÃÁ¬½Ó
public static function reset_connect(){
self::$_instance = null;
}
}
?>
test.php
<?php
require 'db.php';
// Êý¾Ý¿aÉe¶¨
$config = array(
'host' => 'localhost',
'dbname' => 'user',
'user' => 'root',
'password' => '',
'pconnect' => 0,
'charset' => ''
);
// Ñ»*Ö´ÐÐ
while(true){
// ´´½¨Êý¾ÝÁ¬½Ó
$dbconn = DB::get_conn($config);
// ÅжÏÁ¬½ÓÊÇ*ñÓÐЧ
$status = pdo_ping($dbconn);
if($status){
echo 'connect ok'.PHP_EOL;
}else{
echo 'connect failure'.PHP_EOL;
// ÖØÖÃÁ¬½Ó
DB::reset_connect();
$dbconn = DB::get_conn($config);
}
// Ö´Ðвeѯ
$sqlstr = 'select * from user where id=?';
$condparam = array(mt_rand(1,3));
$data = DB::query($dbconn, $sqlstr, $condparam);
print_r($data);
// ÑÓʱ10Ãe
echo 'sleep 10'.PHP_EOL.PHP_EOL;
sleep(10);
}
/**
* ¼i²eÁ¬½ÓÊÇ*ñ¿ÉÓÃ
* @param Link $dbconn Êý¾Ý¿aÁ¬½Ó
* @return Boolean
*/
function pdo_ping($dbconn){
try{
$dbconn->getAttribute(PDO::ATTR_SERVER_INFO);
} catch (PDOException $e) {
if(strpos($e->getMessage(), 'MySQL server has gone away')!==false){
return false;
}
}
return true;
}
?>
4¡¢Ö´ÐÐ
ÔÚphp cliģʽÏÂÖ´ÐÐtest.php£¬È»ºoÂiÉÏÖ´ÐÐmysql.server stop Óe mysql.server start Ä£ÄaÉÁ¶Ï
mysql.server stop
Shutting down MySQL
.... SUCCESS!
mysql.server start
Starting MySQL
SUCCESS!
Ö´ÐÐÊa³o£º
connect ok
Array
(
[0] => Array
(
[id] => 2
[name] => xfdipzone
)
)
sleep 10
connect failure
Array
(
[0] => Array
(
[id] => 3
[name] => terry
)
)
sleep 10
connect ok
Array
(
[0] => Array
(
[id] => 3
[name] => terry
)
)
sleep 10
¿ÉÒÔ¿´µ½ÉÁ¶Ïºo£¬pdo_ping()*µ»Øfalse£¬Á¬½Óʧ°Ü£¬È»ºoµ÷ÓÃ×Ô¶¯ÖØÁ¬£¬±£Ö¤ºoÃaeµÄ²eѯÄܼÌÐøÖ´ÐС£
ÒÔÉÏÕaƪphp PDOÅжÏÁ¬½ÓÊÇñ¿ÉÓõÄʵÏÖ½¨¾ÍÊÇС±aÖÏi¸ø´o¼ÒµÄÈ«²¿ÄÚÈÝÁË£¬Ï£ÍuÄܸø´o¼ÒÒ»¸o²Î¿¼£¬Ò²Ï£Íu´o¼Ò¶a¶aÖ§³Ö½Å±¾Ö®¼Ò¡£