duancheng3042 2018-10-11 18:57
浏览 37

Laravel DB Max Connections错误在单个GET请求中抛出两次

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.

html with error to the user

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?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法
    • ¥15 可否在不同线程中调用封装数据库操作的类