title: Why have I got MySQL sleeping connections when not using mysql_pconnect()
body: <p>An Apache Bench test revealed that high throughput on the database caused the following...</p>
  
  <ul>
  <li>Apache threads stuck in "Sending Reply" state, all related to a particular PHP file (as seen in Apache extended status).</li>
  <li>MySQL sleeping connections with the same user as used by the PHP file.</li>
  </ul>
  
  <p>Note: Apache Bench was used from a remote location as a mechanism for stressing the database only, ie a script just connected to the DB and ran 5 queries per load.</p>
  
  <p>Running the same Apache Bench tests, but introducing a <code>mysql_close()</code> at the end of the script solved the problem. What I'd like to understand is why this happens.</p>
  
  <p>A popular theory internally we have is that:</p>
  
  <ol>
  <li>The increased throughput on the database somehow prevented Apache from serving its requests properly. A buffer somewhere, either in MySQL or at the OS level got filled up.</li>
  <li>The Apache requests therefore got stuck in a Sending Reply state, and because there was no <code>mysql_close()</code> at the end of the PHP file a database connection remained open - probably only to expire once MySQL's connection timeout limit was reached and the MySQL connection was closed. <strong>To be clear, we are not using <code>mysql_pconnect()</code></strong>, but we are using <code>mysql_connect()</code>.</li>
  </ol>
  
  <p>As above, whilst we've 'solved' the problem we'd love to get to the bottom of this with a solid answer rather than an educated guess.</p>
  
  <p>Anyone experienced this before and know of a solution? Any tricks/thoughts/methods of identifying this.</p>