title: PHP to MySQL connection: wait until a connection is available, instead of throwing "max_user_connections exceeded" error?
body: <p>MySQL has a feature that enforces a maximum number of connections, <a href="http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_max_user_connections" rel="nofollow noreferrer">max_user_connections</a>.</p>
  
  <p>When connecting from PHP (and <a href="https://stackoverflow.com/questions/10371199/mysql-has-exceeded-the-max-user-connections-error-on-c-sharp-website">other languages</a>), in the event you exceed that number, my understanding is that your connection will fail with an error stating that you have exceeded the maximum number of connections. Correct?</p>
  
  <p>My preference would be to instead have the connect call wait (up to some timeout limit) for a connection to become available. However, I have not been able to determine a way to do this from reading through the MySQL docs and searching the web.</p>
  
  <p>My concern is that, in the event we have a flood of traffic to our web app that resulted in a large number of concurrent MySQL connection attempts, some of our users scripts will end with an error. Naturally, we could modify the calling code to try to re-connect up to a certain number of times, but it would much cleaner if we could modify the connection attempt itself to handle this, instead of having to wrap every "connect" call in a loop. Additionally, a loop would not result in a real FIFO queue, because each individual calling thread would wait a small period of time and try again, and whether it got a connection would depend on whether, at that particular moment, a connection was available. It would then wait again, and while it was waiting a connection might open and a different thread "further back in line" might grab it.</p>
  
  <p>So, when using any of the PHP APIs to connect to MySQL, is there a way to attempt the connection in "wait until a connection is available" fashion?</p>