What is the best way to handle “Warning: mysql_connect(): Too many connections” with Kohana 3?-Collection of common programming errors

I’ve got a web application that is used by a company for logging their employees’ work.

A lot of people are often logged in at once.

The application runs on a shared host.

I sometimes receive…

Warning: mysql_connect() [function.mysql-connect]: Too many connections

Which then lets further errors cascade… like errors with mysql_select_db(), mysql_error(), mysql_errnon() and finally the uncaught Database_Eexception.

When I run my main request, I wrap it in a try and capture any exception and display a not found page. This is because usually my controllers throw exceptions if a resource is not found (though the route may be valid) e.g. http://example.com/products/30 is a valid route, but product #30 doesn’t exist.

What is the best way to handle the too many connections? Ideally I’d like to capture that exception separately, then display a nice page that informs the employee to try again in 5 minutes.

The code that runs my main request in application/bootstrap.php looks like this…

$request = Request::instance();

try {
    $request->execute();
} catch (Exception $e) {

    if (Kohana::$environment === Kohana::DEVELOPMENT) throw $e;

    // Log the error
    Kohana::$log->add(Kohana::ERROR, Kohana::exception_text($e));

    // Create a 404 response
    $request->status = 404;
    $request->response = Request::factory(Route::get('catch_all')->uri(array('path' => 'errors/404')))->execute();
}

$request->send_headers();
echo $request->response;

Thanks for any help!

  1. I just created such file to handle all the errors:

Originally posted 2013-11-27 12:27:01.