1

这是问题

有一个脚本在 X 时间(5 到 40 分钟之间的未知时间)后抛出以下错误:MySQL 服务器已消失,Kohana 变成Database_Exception 2006结果某些信息未保存到数据库。

这是我认为可能有效的方法

class Model_Bar extends ORM {

protected $_belongs_to = array(
    'foo' => array()
);

public function save(){ //Extends the save method
    try {
        $result = parent::save(); //Try parent save
    }  catch (Database_Exception $e) { //Catch exception
        if ($e->getCode() == 2006) { //If exception code == 2006 then DB has gone away
            mysqli_ping(); //Try to refresh DB link
            $result = parent::save(); //Try parent save again
        } else { //Exception code != 2006
            throw new Exception($e); //Throw new DB exception
        }
    }
    return $result; // Return the result from parent::save()
    }
}

问题:如何刷新 Kohana 的 ORM 中的 DB 链接?

更多信息:

谢谢!

4

1 回答 1

0

这是任何一方的问题

  • 达到MYSQL配置的超时时间
  • 超过数据包大小
  • 丢包

如果您正在执行长插入,如批量插入,如果您的数据库没有为此配置,则代码中的任何更改都不会产生任何影响。您可以尝试重新配置您的 MYSQL 实例,然后排除 MYSQL 的责任,然后在您尝试修改代码之后(我怀疑这是问题的根源)。重试保存不会有太大帮助,但会使数据库更加繁忙。

另一件事,如果您使用的是代理(如 HAProxy),请检查其超时。

于 2012-12-28T09:19:47.437 回答