Reputation: 7531
I'm unit testing a Zend Framework based project with PHPUnit, it seems like there's too many tests.
Symptoms : I'm writing another test and launching this test on this specific function caused the test to fail with error
Stacktrace :
Zend_Db_Adapter_Exception: SQLSTATE[08004] [1040] Too many connections in /Users/MyUser/workspaces/gitrepo/myprojectcom/webapp/library/Zend/Db/Adapter/Pdo/Abstract.php on line 144
Call Stack:
0.0002 639184 1. {main}() /Users/MyUser/pear/bin/phpunit:0
0.0036 1243552 2. PHPUnit_TextUI_Command::main() /Users/MyUser/pear/bin/phpunit:46
0.0036 1244568 3. PHPUnit_TextUI_Command->run() /Users/MyUser/pear/share/pear/PHPUnit/TextUI/Command.php:130
0.0036 1244568 4. PHPUnit_TextUI_Command->handleArguments() /Users/MyUser/pear/share/pear/PHPUnit/TextUI/Command.php:139
0.0202 5745936 5. PHPUnit_Util_Configuration->getTestSuiteConfiguration() /Users/MyUser/pear/share/pear/PHPUnit/TextUI/Command.php:671
0.0202 5747448 6. PHPUnit_Util_Configuration->getTestSuite() /Users/MyUser/pear/share/pear/PHPUnit/Util/Configuration.php:768
0.0539 6143840 7. PHPUnit_Framework_TestSuite->addTestFiles() /Users/MyUser/pear/share/pear/PHPUnit/Util/Configuration.php:848
0.8321 26889016 8. PHPUnit_Framework_TestSuite->addTestFile() /Users/MyUser/pear/share/pear/PHPUnit/Framework/TestSuite.php:419
0.8329 27067008 9. PHPUnit_Framework_TestSuite->addTestSuite() /Users/MyUser/pear/share/pear/PHPUnit/Framework/TestSuite.php:392
0.8329 27068424 10. PHPUnit_Framework_TestSuite->__construct() /Users/MyUser/pear/share/pear/PHPUnit/Framework/TestSuite.php:318
0.8554 27706232 11. PHPUnit_Framework_TestSuite->addTestMethod() /Users/MyUser/pear/share/pear/PHPUnit/Framework/TestSuite.php:214
0.8555 27706384 12. PHPUnit_Framework_TestSuite::createTest() /Users/MyUser/pear/share/pear/PHPUnit/Framework/TestSuite.php:831
0.8556 27719664 13. ControllerTestCaseAbstract->__construct() /Users/MyUser/pear/share/pear/PHPUnit/Framework/TestSuite.php:554
0.8586 27775696 14. Zend_Application_Bootstrap_BootstrapAbstract->bootstrap() /Users/MyUser/workspaces/gitrepo/myprojectcom/webapp/tests/ControllerTestCaseAbstract.php:24
0.8586 27775696 15. Zend_Application_Bootstrap_BootstrapAbstract->_bootstrap() /Users/MyUser/workspaces/gitrepo/myprojectcom/webapp/library/Zend/Application/Bootstrap/BootstrapAbstract.php:586
0.8586 27775696 16. Zend_Application_Bootstrap_BootstrapAbstract->_executeResource() /Users/MyUser/workspaces/gitrepo/myprojectcom/webapp/library/Zend/Application/Bootstrap/BootstrapAbstract.php:629
0.8586 27776040 17. Bootstrap->_initDb() /Users/MyUser/workspaces/gitrepo/myprojectcom/webapp/library/Zend/Application/Bootstrap/BootstrapAbstract.php:669
0.8590 27782440 18. Zend_Db_Adapter_Abstract->getConnection() /Users/MyUser/workspaces/gitrepo/myprojectcom/webapp/application/Bootstrap.php:97
0.8590 27782440 19. Zend_Db_Adapter_Pdo_Mysql->_connect() /Users/MyUser/workspaces/gitrepo/myprojectcom/webapp/library/Zend/Db/Adapter/Abstract.php:315
0.8591 27782440 20. Zend_Db_Adapter_Pdo_Abstract->_connect() /Users/MyUser/workspaces/gitrepo/myprojectcom/webapp/library/Zend/Db/Adapter/Pdo/Mysql.php:109
To avoid doubt here's my database bootstrap function:
protected function _initDb() {
$databaseConfig = $this->_getDbConfig(); //loaded from an .ini file
$resource = Zend_Db::factory($databaseConfig);
$resource->getConnection();
Zend_Db_Table::setDefaultAdapter($resource);
return $resource;
}
Various other elements :
What I'm looking for : any explanation and above all a workaround
Workaround already found : adding a dirty fix in the [mysqld] of /Applications/XAMPP/etc/my.cnf :
set-variable=max_connections=200
Upvotes: 2
Views: 3154
Reputation: 14953
You should use closeConnection() from Zend_Db_Adapter.
When you write your web app, there is (usually) no need to explicitly close the connection, because your script starts, executes few queries and ends clearing out all opened connections. In case of your test, you most likely have some long running script, which opens many connections, but since script is not ended, those connections are not cleared out, so yu will have to close them explicitly when you don't need them.
Upvotes: 1