title: Symfony 4 worker using doctrine not working properly : SQLSTATE[HY000] [2002] Connection timed out
body: <p>I'm using a worker with the Symfony 4 messenger component.</p>
  
  <p>This worker is</p>
  
  <ul>
  <li>receiving a message (from rabbitMQ)</li>
  <li>launch ffmpeg</li>
  <li>do a treatment on a video</li>
  <li>and save something in a database.</li>
  </ul>
  
  <p>To configure this worker on Symfony I've done this (middleware are important):</p>
  
  <pre class="lang-none prettyprint-override"><code>// config/packages/framework.yaml
  framework:
      messenger:
          buses:
              command_bus:
                  middleware:
                      # each time a message is handled, the Doctrine connection
                      # is "pinged" and reconnected if it's closed. Useful
                      # if your workers run for a long time and the database
                      # connection is sometimes lost
                      - doctrine_ping_connection
  
                      # After handling, the Doctrine connection is closed,
                      # which can free up database connections in a worker,
                      # instead of keeping them open forever
                      - doctrine_close_connection
  
          transports:
              ffmpeg:
                dsn: '%env(CLOUDAMQP_URL)%'
                options:
                  auto_setup: false
                  exchange:
                      name: amq.topic
                      type: topic
                  queues:
                    ffmpeg: ~
  
          routing:
              # Route your messages to the transports, for now all are AMQP messages
              'App\Api\Message\AMQPvideoFFMPEG': ffmpeg
          ## Handle multiple buses ? https://symfony.com/doc/current/messenger/multiple_buses.html
          ## When queries and command should be distinguished
  </code></pre>
  
  <p>Then in order to understand what may cause this issue I've try to debug the messenger to see if the middleware are correctly configured</p>
  
  <pre class="lang-none prettyprint-override"><code>root@b9eec429cb54:/var/www/html# php bin/console debug:messenger
  
  Messenger
  =========
  
  command_bus
  -----------
  
   The following messages can be dispatched:
  
   ------------------------------------------------------ 
    App\Api\Message\AMQPvideoFFMPEG                       
        handled by App\Api\Message\Handler\FFMPEGHandler  
   ------------------------------------------------------ 
  
  </code></pre>
  
  <p>Everything seems ok right ?</p>
  
  <p>So how is this possible to see this :</p>
  
  <blockquote>
    <p>[2019-08-23 10:25:26] messenger.ERROR: Retrying App\Api\Message\AMQPvideoFFMPEG - retry #1. {"message":"[object] (App\Api\Message\AMQPvideoFFMPEG: {})","class":"App\Api\Message\AMQPvideoFFMPEG","retryCount":1,"error":"[object] (Doctrine\DBAL\Exception\ConnectionException(code: 0): An exception occurred in driver: SQLSTATE[HY000] [2002] Connection timed out at /var/www/html/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php:93, Doctrine\DBAL\Driver\PDOException(code: 2002): SQLSTATE[HY000] [2002] Connection timed out at /var/www/html/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:31, PDOException(code: 2002): SQLSTATE[HY000] [2002] Connection timed out at /var/www/html/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:27)"} []</p>
  </blockquote>
  
  <p>I'm completely lost, Have I missed something ?</p>
  
  <p>This happens sometimes, but it works most of the time, I suppose this bug happen when my worker has lost the connection to DB especially if ffmpeg treatment last 7 minutes or higher, but this should be avoided by the ping and the close connection's middlewares. So i don't clearly understand what is the problem here.</p>