Skip to content

Instantly share code, notes, and snippets.

@pigfly88
Created September 19, 2019 06:26
Show Gist options
  • Save pigfly88/8703cb608df0ea992868d6cd73287b8b to your computer and use it in GitHub Desktop.
Save pigfly88/8703cb608df0ea992868d6cd73287b8b to your computer and use it in GitHub Desktop.
doctrine fix mysql server gone away
try {
$this->em->getConnection()->fetchAll("select * from tbl where id=1");
sleep(30); // set this bigger than mysql wait_timeout
$this->em->getConnection()->fetchAll("select * from tbl where id=2"); // and then you will see mysql has gone away here
} catch (DBALException $exception) { // don't worry, let's catch this exception and reconnect to mysql
if (false !== strpos($exception->getPrevious()->getMessage(), 'server has gone away')) {
$this->em->getConnection()->close();
$this->em->getConnection()->->connect();
// or use $this->container->get('doctrine.orm.entity_manager') if you don't have $this->em
// redo job
$this->em->getConnection()->fetchAll("select * from tbl where id=2");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment