title: What is the best way to handle "Warning: mysql_connect(): Too many connections" with Kohana 3?
body: <p>I've got a web application that is used by a company for logging their employees' work.</p>
  
  <p>A lot of people are often logged in at once.</p>
  
  <p>The application runs on a shared host.</p>
  
  <p>I sometimes receive...</p>
  
  <blockquote>
    <p>Warning: mysql_connect() [function.mysql-connect]: Too many connections</p>
  </blockquote>
  
  <p>Which then lets further errors cascade... like errors with <code>mysql_select_db()</code>, <code>mysql_error()</code>, <code>mysql_errnon()</code> and finally the uncaught <code>Database_Eexception</code>.</p>
  
  <p>When I run my main request, I wrap it in a <code>try</code> and capture any exception and display a <em>not found</em> page. This is because usually my controllers throw exceptions if a resource is not found (though the route may be valid) e.g. <code>http://example.com/products/30</code> is a valid route, but product #30 doesn't exist.</p>
  
  <p>What is the best way to handle the <em>too many connections</em>? Ideally I'd like to capture that exception separately, then display a nice page that informs the employee to try again in 5 minutes.</p>
  
  <p>The code that runs my main request in <code>application/bootstrap.php</code> looks like this...</p>
  
  <pre><code>$request = Request::instance();
  
  try {
      $request-&gt;execute();
  } catch (Exception $e) {
  
      if (Kohana::$environment === Kohana::DEVELOPMENT) throw $e;
  
      // Log the error
      Kohana::$log-&gt;add(Kohana::ERROR, Kohana::exception_text($e));
  
      // Create a 404 response
      $request-&gt;status = 404;
      $request-&gt;response = Request::factory(Route::get('catch_all')-&gt;uri(array('path' =&gt; 'errors/404')))-&gt;execute();
  }
  
  $request-&gt;send_headers();
  echo $request-&gt;response;
  </code></pre>
  
  <p>Thanks for any help!</p>