Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to handle MySQL deadlock error in CodeIgniter 3.x

How to handle MySQL deadlock error in CodeIgniter 3.x
Tried normal try catch option , but its not working

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here are the steps to handle mySQL deadlock in Codeigniter-3.

  1. Enable the $config[‘db_debug’] = TRUE
  2. Catch the deadlock error using try-catch. Use this CI_DB_mysqli_utility::DB_ERROR_DEADLOCK constant in Codeigniter to catch the error.

Sample code:

try{
    //mysql query execution
 }
 catch(Exception $e){
    if ($e->getCode() == CI_DB_mysqli_utility::DB_ERROR_DEADLOCK) {
        // Handle deadlock error
        // For example, retry the operation after a delay
        sleep(2); // Sleep for 2 seconds before retrying
        // Retry the database operation
    } else {
        // Handle other database errors
        log_message('error', 'Database error: ' . $e->getMessage());
        // Throw or handle the error as needed
    }
}

These steps worked out for me.
———————— New Edit ———————————–
If you can’t edit the config value in production. You can set the config dynamically within your method by using the below function.

function set_db_debug($enable_db_debug) {
    $CI =& get_instance();
    $CI->config->load('config');
    $CI->config->set_item('db_debug', $enable_db_debug);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading