在 Gateway 里面,获取 SQL 连接的代码:
$ident = md5($dsn);
try {
if(!empty(self::$instances)){
$message = self::$instances->getAttribute(PDO::ATTR_SERVER_INFO);
if(strpos($message, 'Lost connection to MySQL server during query') !== FALSE || strpos($message, 'MySQL server has gone away') !== FALSE){
throw new \PDOException("MySQL server has gone away!");
}
}else{
self::$instances = new PDO($dsn, Config::DB_USERNAME, Config::DB_PASSWORD, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET CHARACTER SET utf8;', PDO::ATTR_PERSISTENT => Config::DB_ATTR_PERSISTENT));
self::$instances->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if (Config::DB_SET_TIMEZONE == TRUE) {
static::setDbTimezone(Config::DB_TIMEZONE, self::$instances);
}
}
} catch (\Exception $e) {
Log::getLogger()->error($e->getMessage());
self::$instances = NULL;
self::$instances = new PDO($dsn, Config::DB_USERNAME, Config::DB_PASSWORD, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET CHARACTER SET utf8;', PDO::ATTR_PERSISTENT => Config::DB_ATTR_PERSISTENT));
self::$instances->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if (Config::DB_SET_TIMEZONE == TRUE) {
static::setDbTimezone(Config::DB_TIMEZONE, self::$instances);
}
}
return self::$instances;
把 Mysql 的 wait_timeout 设置 6 秒,执行跑脚本,sleep(7) 之后,在获取pdo ,会直接抛出异常;
但是, 把这个代码放在 Gateway 环境里面,设置估计7秒之后,才去获取 pdo,没有抛出异常,而是 PDO::getAttribute 直接返回了 “Lost connection to MySQL server during query”, 这是什么梗?