我正在使用PHPUnit对基于Zend Framework的项目进行单元测试,看起来测试太多了。
症状:我正在编写另一个测试并在此特定函数上启动此测试导致测试失败并显示错误
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
为了避免怀疑,这是我的数据库引导函数:
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;
}
各种其他元素:
我在寻找什么:任何解释,最重要的是解决方法
已找到解决方法:在/Applications/XAMPP/etc/my.cnf的[mysqld]中添加一个脏修复程序:
set-variable=max_connections=200
答案 0 :(得分:1)
您应该使用Zend_Db_Adapter中的closeConnection()。
当您编写Web应用程序时,(通常)不需要显式关闭连接,因为您的脚本启动,执行少量查询并结束清除所有打开的连接。在您进行测试的情况下,您很可能有一些长时间运行的脚本,这会打开许多连接,但由于脚本未结束,因此不会清除这些连接,因此当您不需要时,yu必须显式关闭它们。