Handle lock wait timeout #15920
Replies: 3 comments 10 replies
-
This is one of the most difficult bugs to find sadly, especially in large applications. From what I have seen before, a transaction opened somewhere and never committed or rolled back. One way to do it but it is painful honestly especially in production, is to implement a query listener which will log all the transactions you create and any commits or rollbacks. Events for the db layer: https://docs.phalcon.io/4.0/en/db-layer#events You can create a listener class like this: <?php
namespace MyNamespace;
use Phalcon\Db\Adapter\AdapterInterface;
use Phalcon\Events\Event;
use Phalcon\Logger\Logger;
use Phalcon\Di\Injectable;
/**
* @property Logger $logger
*/
class QueryListener extends Injectable
{
/**
* @param Event $event
* @param AdapterInterface $connection
*/
public function beginTransaction(/Event $event, AdapterInterface $connection)
{
$this->logger->warning(
sprintf(
'BEGIN: %s - [%s]',
$connection->getSQLStatement(),
json_encode($connection->getSQLVariables())
)
);
}
/**
* @param Event $event
* @param AdapterInterface $connection
*/
public function commitTransaction(/Event $event, AdapterInterface $connection)
{
$this->logger->warning(
sprintf(
'COMMIT: %s - [%s]',
$connection->getSQLStatement(),
json_encode($connection->getSQLVariables())
)
);
}
/**
* @param Event $event
* @param AdapterInterface $connection
*/
public function rollbackTransaction(/Event $event, AdapterInterface $connection)
{
$this->logger->warning(
sprintf(
'ROLLBACK: %s - [%s]',
$connection->getSQLStatement(),
json_encode($connection->getSQLVariables())
)
);
}
} and attach the listener to your db connection. $eventsManager = $container->getShared('eventsManager');
$eventsManager->attach(
'db',
new QueryListener(),
90
); The |
Beta Was this translation helpful? Give feedback.
-
Thank you @niden . This is good, I will try it. |
Beta Was this translation helpful? Give feedback.
-
Hi @niden , from the log created, how to analyse it? Is it I need to find transaction which don't have COMMIT after BEGIN? |
Beta Was this translation helpful? Give feedback.
-
Hi, my application usually received this error:
SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction
Its hard to debug because cannot I replicate the issue at local. Only happens sometime at production.
How to debug the error? And what is the best way to fix or handle it?
Thanks.
Beta Was this translation helpful? Give feedback.
All reactions