Created
September 19, 2019 06:26
-
-
Save pigfly88/8703cb608df0ea992868d6cd73287b8b to your computer and use it in GitHub Desktop.
doctrine fix mysql server gone away
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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