Example #1
0
 /**
  * Special handling for PDO query().
  * All bind parameter names must begin with ':'.
  *
  * @param string|\Zend_Db_Select $sql The SQL statement with placeholders.
  * @param mixed $bind An array of data or data itself to bind to the placeholders.
  * @return \Zend_Db_Statement_Pdo|void
  * @throws \Zend_Db_Adapter_Exception To re-throw \PDOException.
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function query($sql, $bind = [])
 {
     $connectionErrors = [2006, 2013];
     $triesCount = 0;
     do {
         $retry = false;
         $this->logger->startTimer();
         try {
             $this->_checkDdlTransaction($sql);
             $this->_prepareQuery($sql, $bind);
             $result = parent::query($sql, $bind);
             $this->logger->logStats(LoggerInterface::TYPE_QUERY, $sql, $bind, $result);
             return $result;
         } catch (\Exception $e) {
             // Finalize broken query
             $profiler = $this->getProfiler();
             if ($profiler instanceof Profiler) {
                 /** @var Profiler $profiler */
                 $profiler->queryEndLast();
             }
             /** @var $pdoException \PDOException */
             $pdoException = null;
             if ($e instanceof \PDOException) {
                 $pdoException = $e;
             } elseif ($e instanceof \Zend_Db_Statement_Exception && $e->getPrevious() instanceof \PDOException) {
                 $pdoException = $e->getPrevious();
             }
             // Check to reconnect
             if ($pdoException && $triesCount < self::MAX_CONNECTION_RETRIES && in_array($pdoException->errorInfo[1], $connectionErrors)) {
                 $retry = true;
                 $triesCount++;
                 $this->closeConnection();
                 $this->_connect();
             }
             if (!$retry) {
                 $this->logger->logStats(LoggerInterface::TYPE_QUERY, $sql, $bind);
                 $this->logger->critical($e);
                 throw $e;
             }
         }
     } while ($retry);
 }