I find a frequently error when working on Mangeto sites with high volume of transactions, a big catalog (ex. >20000 products) or a lot of cron jobs updating database with direct queries.

Exception SQLSTATE[40001]: Serialization failure: 1213 Deadlock found when trying to get lock; try restarting transaction

First we need to rewrite the Pdo script to our local folder so we need to download the file /lib/Zend/Db/Statement/Pdo.php

and upload to /app/code/local/Zend/Db/Statement/Pdo.php

Now edit this new file inside local and replace the function _execute in line 224

public function _execute(array $params = null)
    {
        try {
            if ($params !== null) {
                return $this->_stmt->execute($params);
            } else {
                return $this->_stmt->execute();
            }
        } catch (PDOException $e) {
            #require_once 'Zend/Db/Statement/Exception.php';
            throw new Zend_Db_Statement_Exception($e->getMessage(), (int) $e->getCode(), $e);
        }
    }

with this :

public function _execute(array $params = null)
    {
        $max_tries = 3;
        $tries = 0;
        do
        {
            $retry = false;
            try
            {
                if ($params !== null)
                {
                    return $this->_stmt->execute($params);
                } else
                {
                    return $this->_stmt->execute();
                }
            }
            catch (PDOException $e)
            {
                if ($tries < $max_tries && preg_match('/SQLSTATE\[40001\]/', $e->getMessage()))
                {
                    //Mage::log('deadlock at '.date('Y-m-d h:i:s'), null, 'deadlock.log');
                    $retry = true;
                    sleep(1);
                } else
                {
                    throw new Zend_Db_Statement_Exception($e->getMessage(), (int)$e->getCode(), $e);
                }
                $tries++;
            }
        } while ($retry);
    }

Basically this script try to re-run the SQLSTATE[40001] failed transaction 3 times leaving 1 second between the tries.