title: PHP ZF2 Mysql has gone away
body: <p>Hi I have a php daemon that handle request from rabbitmq</p>
  
  <p>After a day, it can no longer execute due to error MySQL has gone away.</p>
  
  <pre><code>PHP Warning:  PDOStatement::execute(): MySQL server has gone away in /var/www/daemon/www/vendor/zendframework/zendframework/library/Zend/Db/Adapter/Driver/Pdo/Statement.php on line 239
  PHP Warning:  PDOStatement::execute(): Error reading result set\'s header in /var/www/daemon/www/vendor/zendframework/zendframework/library/Zend/Db/Adapter/Driver/Pdo/Statement.php on line 239
  </code></pre>
  
  <p>I didn't use doctrine, instead I send my \Zend\Db\Adapter\Adapter to a db wrapper class with below function.</p>
  
  <pre><code>public static function executeScalar($statement, $parameters, \Zend\Db\Adapter\Adapter $dbAdapter)
  {
      $dbResult = new DbResult();
      if (! $statement) {
          $dbResult-&gt;addError('No statement given');
          return $dbResult;
      }
  
      $stmt = $dbAdapter-&gt;createStatement();
      $stmt-&gt;prepare($statement);
  
      foreach ($parameters as $key =&gt; &amp;$param) {
          $stmt-&gt;getResource()-&gt;bindParam($key + 1, $param[0], $param[1]);
      }
  
      try {
          $result = $stmt-&gt;execute();
          $dbResult-&gt;setResult($result);
      } catch (\Zend\Db\Adapter\ExceptionInterface $e) {
          $dbResult-&gt;addError('DB Error');
          $message = $e-&gt;getPrevious() ? $e-&gt;getPrevious()-&gt;getMessage() : $e-&gt;getMessage();
          $dbResult-&gt;addError($message);
      } catch (\Zend\Db\Adapter\Exception $e) {
          $dbResult-&gt;addError('DB Error');
          $dbResult-&gt;addError($e-&gt;getMessage());
      } catch (\PDOException $e) {
          $dbResult-&gt;addError('DB Error');
          $dbResult-&gt;addError($e-&gt;getMessage());
      } catch (\Exception $e) {
          $dbResult-&gt;addError('DB Error');
          $dbResult-&gt;addError($e-&gt;getMessage());
      }
      $stmt-&gt;getResource()-&gt;closeCursor();
  
      return $dbResult;
  }
  </code></pre>
  
  <p>DbResult is my own db result wrapper class it mainly check whether it return empty, what's the error, how many rows, etc.</p>
  
  <p>Here is my database.local.php configuration</p>
  
  <pre><code>return array(
      'service_manager' =&gt; array(
      'factories' =&gt; array(
          'mysql' =&gt; function ($sm)
          {
              return new Zend\Db\Adapter\Adapter(array(
                  'driver' =&gt; 'PdoMysql',
                  'hostname' =&gt; 'localhost',
                  'database' =&gt; 'daemon',
                  'username' =&gt; 'daemon',
                  'password' =&gt; 'password',
                  'driver_options' =&gt; array(
                      \PDO::MYSQL_ATTR_INIT_COMMAND =&gt; 'SET NAMES \'UTF8\'',
                      \PDO::ATTR_ERRMODE =&gt; \PDO::ERRMODE_EXCEPTION,
                      \PDO::ATTR_EMULATE_PREPARES =&gt; true,
                      \PDO::MYSQL_ATTR_LOCAL_INFILE =&gt; true
                  )
              ));
          },
      )
      )
  )
  </code></pre>
  
  <p>So everytime I want to execute a sql I do this inside controller or any other class ( Just an example )</p>
  
  <pre><code>$service = $this-&gt;getServiceLocator();
  $dbAdapter = $service-&gt;get('mysql');
  $get = \Db\Database::executeScalar('SELECT * FROM mytable WHERE id &lt;= ?', array(10), $dbAdapter); 
  </code></pre>
  
  <p>It seems I cannot catch the warning, and is there a way to force reconnect or perhaps I just do a disconnect after each request ?</p>
  
  <p>Will this works, to handle the error ?
  On every new request I do this</p>
  
  <pre><code>$dbAdapter-&gt;getDriver()-&gt;getConnection()-&gt;connect();
  </code></pre>
  
  <p>At the end of request I do this</p>
  
  <pre><code>$dbAdapter-&gt;getDriver()-&gt;getConnection()-&gt;disconnect();
  </code></pre>