Last active
November 8, 2021 14:41
-
-
Save ludofleury/5074595 to your computer and use it in GitHub Desktop.
A Behat Context with a hook to kill the Mysql connections
This file contains 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
<?php | |
use Behat\Symfony2Extension\Context\KernelAwareInterface; | |
use Behat\Symfony2Extension\Context\KernelDictionary; | |
use Behat\MinkExtension\Context\MinkContext; | |
class MysqlContext extends MinkContext implements KernelAwareInterface | |
{ | |
use KernelDictionary; | |
/** | |
* @AfterScenario | |
*/ | |
public function killMysqlConnections() | |
{ | |
$connections = $this->kernel->getContainer()->get('doctrine')->getConnections(); | |
foreach ($connections as $connection) { | |
$threads = $connection->query('SHOW FULL PROCESSLIST'); | |
while ($thread = $threads->fetch()) { | |
if ($thread['db'] == $connection->getDatabase() && $thread['Command'] == 'Sleep') { | |
$connection->query(sprintf('KILL %d', $thread['Id'])); | |
} | |
} | |
} | |
} | |
} |
You should use a shared connection instead and use a "BeforeScenario" hook to set the shared connection on the container, i suppose that would be much faster than reconnecting every time.
Thanks @beberlei for the tips, I'll try.
Thanks soooooo much @ludofleury. You saved my day after a Symfony 2.5 / PHP 5.5 upgrade! ;)
that was a life saver! I thought that unsetting the PDO connection was enough but it was not.
thanks for this
You’re welcome, happy to help!
On Tue 25 Feb 2020 at 14:23, Javi Aguilar ***@***.***> wrote:
that was a life saver! I thought that unsetting the PDO connection was
enough but it was not.
thanks for this
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<https://gist.github.com/5074595?email_source=notifications&email_token=AABNVHXWJKETB5YYD7QB56LREULVRA5CNFSM4K3IZK32YY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAGCT4O#gistcomment-3188679>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABNVHXUMM5CZVOCTTKNESDREULVRANCNFSM4K3IZK3Q>
.
--
*Ludovic Fleury*
Paris, France.
Budapest, Hungary.
+33 6 18 50 47 24
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In a very large Behat suite, you could have this problem with Doctrine: too many connections.
If you're using Mysql, this hook will kill all sleeping connections related to your test database.
Remember to check SymfonyDoctrineContext which also try to close connection after a scenario. (Yet some connections still up and the stack could grow over your hardware limit).