samedi 3 décembre 2022

Lumen queue throwing MySQL server has gone away error

I'm calling a common function from Lumen queue and cron job. During user interaction this function is called from queue ( async purpose ), if something goes wrong with queue execution for example lock wait timeout etc. I have a cron job which is scheduled for every 15 mins to process older transactions. There is no issue when cron is executed but whenever queue is executed MySQL server has gone away error is occurring. Error is occurring at the line DB::connection()->getpdo()->exec('BEGIN');

I did some research on internet, those articles are saying this kind of error will raise when we try to insert large data and we can avoid this error my increasing max_allowed_packet. But error is occurring at beginning line itself, I'm not trying to insert large data ( mostly it should be in KBs ) and moreover same function is working when cron executes for every 15 mins. Following are my code snippets,

public function processTransaction($data)
{
    try {
        $this->validate($data);
        DB::connection()->getpdo()->exec('BEGIN');
    // Save & Update DB 
        DB::connection()->getpdo()->exec('COMMIT');
    } catch (Exception $ex) {
        DB::connection()->getpdo()->exec('ROLLBACK');
        Log::error($ex->getMessage() . '-' . $ex->getTraceAsString());
        throw new AppException($ex->getMessage(), $ex->getCode());
    }
}

Above is the initial version I tried, in this case error was at ROLLBACK statement. Later I had updated to following

public function processTransaction($data)
{
    try {
        $this->validate($data);
        DB::connection()->getpdo()->exec('BEGIN');
    // Save & Update DB 
        DB::connection()->getpdo()->exec('COMMIT');
    } catch (Exception $ex) {
        Log::error($ex->getMessage() . '-' . $ex->getTraceAsString());
        try {
            DB::connection()->getpdo()->exec('ROLLBACK');
        } catch (Exception $ex) {
            Log::error($ex->getMessage() . '-' . $ex->getTraceAsString());
        }
        throw new AppException($ex->getMessage(), $ex->getCode());
    }
}

Here the error is at BEGIN statement and PDO exception error code is in string, which is also creating the issue for argument 2 AppException (extends Exception class), which excepts argument 2 as integer. I think PDO exception issue can be handled by separately catching PDO exception but I want to know why MySQL server has gone error is getting.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire