Handling MySQL deadlock in CodeIgniter 4

25 views Asked by At

Perhaps this is due to a gap in my understanding, but I've been trying to find a solution to the Deadlock issue in MySQL for the past few days. Despite my efforts, I'm still encountering a deadlock. I attempted to manage it using a try-catch block (\Exception $e), but the deadlock error isn't being caught. Does anyone have an alternative method to catch a MySQL deadlock error?

My objective in catching deadlock errors is to implement a retry mechanism whenever a deadlock occurs.

I'm using this CodeIgniter model, but even when a deadlock occurs, the log_message doesn't log anything.

public function updateUserBalance($userid, $balance)
{
    try {
        return $this->db->table($this->user)
            ->set('balance', "balance+{$balance}", false)
            ->where('id', $userid)
            ->update();
    } catch (\Exception $e) {
        // Handle deadlock error
        log_message('error', 'Deadlock occurred: ' . $e->getMessage());
        return false;
    }
}

Any answer is very valuable to me. Thank You

0

There are 0 answers