title: "Too many connections" error in a PHPUnit using Zend Framework
body: <p>I'm unit testing a Zend Framework based project with PHPUnit, it seems like there's too many tests.</p>
  
  <p><strong>Symptoms</strong> : I'm writing another test and launching this test on this specific function caused the test to fail with error </p>
  
  <p><strong>Stacktrace</strong> :</p>
  
  <pre><code>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-&gt;run() /Users/MyUser/pear/share/pear/PHPUnit/TextUI/Command.php:130
      0.0036    1244568   4. PHPUnit_TextUI_Command-&gt;handleArguments() /Users/MyUser/pear/share/pear/PHPUnit/TextUI/Command.php:139
      0.0202    5745936   5. PHPUnit_Util_Configuration-&gt;getTestSuiteConfiguration() /Users/MyUser/pear/share/pear/PHPUnit/TextUI/Command.php:671
      0.0202    5747448   6. PHPUnit_Util_Configuration-&gt;getTestSuite() /Users/MyUser/pear/share/pear/PHPUnit/Util/Configuration.php:768
      0.0539    6143840   7. PHPUnit_Framework_TestSuite-&gt;addTestFiles() /Users/MyUser/pear/share/pear/PHPUnit/Util/Configuration.php:848
      0.8321   26889016   8. PHPUnit_Framework_TestSuite-&gt;addTestFile() /Users/MyUser/pear/share/pear/PHPUnit/Framework/TestSuite.php:419
      0.8329   27067008   9. PHPUnit_Framework_TestSuite-&gt;addTestSuite() /Users/MyUser/pear/share/pear/PHPUnit/Framework/TestSuite.php:392
      0.8329   27068424  10. PHPUnit_Framework_TestSuite-&gt;__construct() /Users/MyUser/pear/share/pear/PHPUnit/Framework/TestSuite.php:318
      0.8554   27706232  11. PHPUnit_Framework_TestSuite-&gt;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-&gt;__construct() /Users/MyUser/pear/share/pear/PHPUnit/Framework/TestSuite.php:554
      0.8586   27775696  14. Zend_Application_Bootstrap_BootstrapAbstract-&gt;bootstrap() /Users/MyUser/workspaces/gitrepo/myprojectcom/webapp/tests/ControllerTestCaseAbstract.php:24
      0.8586   27775696  15. Zend_Application_Bootstrap_BootstrapAbstract-&gt;_bootstrap() /Users/MyUser/workspaces/gitrepo/myprojectcom/webapp/library/Zend/Application/Bootstrap/BootstrapAbstract.php:586
      0.8586   27775696  16. Zend_Application_Bootstrap_BootstrapAbstract-&gt;_executeResource() /Users/MyUser/workspaces/gitrepo/myprojectcom/webapp/library/Zend/Application/Bootstrap/BootstrapAbstract.php:629
      0.8586   27776040  17. Bootstrap-&gt;_initDb() /Users/MyUser/workspaces/gitrepo/myprojectcom/webapp/library/Zend/Application/Bootstrap/BootstrapAbstract.php:669
      0.8590   27782440  18. Zend_Db_Adapter_Abstract-&gt;getConnection() /Users/MyUser/workspaces/gitrepo/myprojectcom/webapp/application/Bootstrap.php:97
      0.8590   27782440  19. Zend_Db_Adapter_Pdo_Mysql-&gt;_connect() /Users/MyUser/workspaces/gitrepo/myprojectcom/webapp/library/Zend/Db/Adapter/Abstract.php:315
      0.8591   27782440  20. Zend_Db_Adapter_Pdo_Abstract-&gt;_connect() /Users/MyUser/workspaces/gitrepo/myprojectcom/webapp/library/Zend/Db/Adapter/Pdo/Mysql.php:109
  </code></pre>
  
  <p>To avoid doubt here's my database bootstrap function:</p>
  
  <pre><code>protected function _initDb() {
      $databaseConfig = $this-&gt;_getDbConfig(); //loaded from an .ini file
      $resource = Zend_Db::factory($databaseConfig);
      $resource-&gt;getConnection();
      Zend_Db_Table::setDefaultAdapter($resource);
      return $resource;
  }
  </code></pre>
  
  <p><strong>Various other elements</strong> :</p>
  
  <ul>
  <li>Commenting one class's test method let me add a this new test case without problem, so the problem doesn't came from my specific new case, but from the amount of test done</li>
  <li>I have a very basic _initDb bootstrap function as you can see</li>
  <li>This fail also when launching the whole test suite without using "--filter"</li>
  <li>My environment is : Mac OS X 10.7.3 Lion, XAMPP's MySQL server 5.1.44, PHPunit 3.6.11 installed through PEAR, PHP 5.3.8</li>
  <li>I do close my connection in the tear down [EDIT]</li>
  </ul>
  
  <p><strong>What I'm looking for</strong> : any explanation and above all a workaround</p>
  
  <p><strong>Workaround already found</strong> : adding a dirty fix in the [mysqld] of /Applications/XAMPP/etc/my.cnf :</p>
  
  <pre><code>set-variable=max_connections=200
  </code></pre>