Pergunta

I have a Symfony command line task that has a habit of dropping the mysql connection.

Its a data import task. Which fetches data from multiple connections. Its not one big query but a few smaller ones.

It seems to drop the connection the first time it is ran. About half way through the script. However the second time its ran (from the beginning) it always completes the task.

Its not timing out on the query as the error response I get is that the connection has been dropped and it runs ok on its own. So im thinking that its some kind of timeout issue that is avoided when its ran the second time due to query caching speeding up the script.

So my question is how do I refresh the database connection?

[Doctrine\DBAL\DBALException]
SQLSTATE[HY000]: General error: 2013 Lost connection to MySQL server during query

Foi útil?

Solução 2

I guess you mean to connect to the database if the connection is lost for some reason. Given an EntityManager, you can do it the following way:

$success = $_em->getConnection()->connect();

With getConnection, you are retrieving the connection object doctrine uses (Doctrine\DBAL\Connection), which exposes the connect method.

You can call connect anytime, as it checks wether a connection is already established. If this is the case, it returns false.

There is also a isConnected method to check wether a connection is established. You could use that to see where exactly the connection is dropping to get a clearer picture of what is happening.

Outras dicas

A different approach is to check if Doctrine is still connected to the MySQL server through the ping() method in the connection. If the connection is lost, close the active connection since it is not really closed yet and start a new one.

if(FALSE == $em->getConnection()->ping()){
    $em->getConnection()->close();
    $em->getConnection()->connect();
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top