i have a proyect in production made with Laravel 5.4 on AWS, with EC2 and RDS (with autoscaling). In a normal day our current configuration doesn't have a problem handling a certain amount of users, but sometimes this number of user grows up enough to start generating this DB Max Connections error [1040], even tho we have autoscaling configured to avoid this (i think that the replica is taking enough time to start while we still have some users trying to access).
So, in the Handler.php file we put this code to show a custom page informing the issue to the user (and not just the generic woops!)
if ($e instanceof \PDOException) {
Log::error('Data Base Exception');
$errCode = $e->getCode();
switch ($errCode) {
case 1040:
return response()->view("errors.database", []);
break;
default:
return response()->view("errors.500", [], 500);
break;
}
}
The problem is that apparently laravel is opening more than 1 connection at the same time even this is only a single GET request (it happens to any request on the proyect), so the Handler.php is reporting it twice. Here is a look at the log file generated when throwing this exception.
[2018-10-11 14:04:09] production.ERROR: Data Base Exception
[2018-10-11 14:04:09] production.ERROR: Data Base Exception
I have tested this on my local enviroment, setting the max_connections attribute from mysql to 1
set global max_connections = 1;
This throws the exception as intented, but the HTML page with the information of the problem appears also twice, just like the log.
Curious thing: when i set the max_connections atributte to 2, the exception is not throwed and laravel process the request normally.
EDIT: it works in this case because i didn't considered that i was using DBeaver to access the database. But the problem of throwing the exception twice persist
The question is: what is laravel doing that need 2 conections even when is just 1 user trying to access? Or maybe this is something related with some kind of reconnection attempt?