Gearman, ZF2, Doctrine2, MySQL, SQLSTATE[HY000]: General error: 2006 MySQL server has gone away

杀马特。学长 韩版系。学妹 提交于 2019-12-05 20:55:38

Each worker script should connect to the database at the start and disconnect at the end. Don't try and hold open the connection as if MySQL times out, then your script won't notice which is why you then get the error.

Here is my solution described in my blog post (in Russian, you can translate with Google).

http://seyferseed.ru/php/reshenie-problemy-doctrine-2-i-mysql-server-has-gone-away.html

I'm using this code for solving problem with MySQL server has gone away

public function disconnect()
{
    $this->getEm()->getConnection()->close();
}

public function connect()
{
    $this->getEm()->getConnection()->connect();
}

/**
 * MySQL Server has gone away
 */
public function reconnect()
{
    $connection = $this->getEm()->getConnection();
    if (!$connection->ping()) {

        Debug::vars("MySQL ping failed");

        $this->disconnect();
        $this->connect();

        $this->checkEMConnection($connection);
    }
}

/**
 * method checks connection and reconnect if needed
 * MySQL Server has gone away
 *
 * @param $connection
 * @throws \Doctrine\ORM\ORMException
 */
protected function checkEMConnection($connection)
{
    if (!$this->getEm()->isOpen()) {
        $config = $this->getEm()->getConfiguration();

        $this->em = $this->getEm()->create(
            $connection, $config
        );
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!