2014-09-05 148 views
2

我不确定这是PHP中的错误,MySQL还是我很愚蠢,但很偶然(并且非常罕见),我们将其称为mysqli::query,并且不会获得FALSEmysqli_result对象。mysqli ::查询不返回FALSE或mysqli_result - PHP的错误?

我们正在运行:

  • PHP 5.5.9-1ubuntu4.3
  • 的MySQL版本15.1 DISTRIB 38年5月5日,MariaDB的,使用readline的5.1 Debian的Linux-GNU(x86_64的)(使用Galera)

查询运行是SHOW STATUS LIKE "Questions"

根据the manualSHOW查询我们应该时刻找回要么FALSEmysqli_result对象,但我们没有得到任何。这行代码每天运行超过10万次,每周大约失败一次。

下面是从我们的数据库封装代码片段(该功能只会被用于读查询):

public function read($query, $cache = 0) { 
    $total_time_start = microtime(1); 

    $deadlock_retries_done = 0; 
    $deadlock_retries_max = 10; 
    $other_error_retries_done = 0; 
    $other_error_retries_max = 10; 

    $return = array(); // Keeps IDEs happy! 

    while (true) { 
     $start = microtime(1); 
     $return = array(); 
     $q = $this->connection->query($query, MYSQLI_STORE_RESULT); 
     $this->debug_last_query = $query; 

     // Was there an error? 
     if ($q === false) { 
      $error = $this->connection->error; 
      switch ($error) { 
       case 'Deadlock found when trying to get lock; try restarting transaction': 
       case 'Lock wait timeout exceeded; try restarting transaction': 
        $deadlock_retries_done++; 
        if ($deadlock_retries_done == $deadlock_retries_max) { 
         throw new SQLException($error . '. Re-tried with deadlock ' . $deadlock_retries_done . ' times. Query: ' . $query); 
        } else { 
         continue; // Try again 
        } 
        break; 

       case 'WSREP has not yet prepared node for application use': 
        $other_error_retries_done++; 
        if ($other_error_retries_done == $other_error_retries_max) { 
         throw new SQLException($error . '. Re-tried with error ' . $other_error_retries_done . ' times. Query: ' . $query); 
        } else { 
         if ($this->in_transaction) { 
          throw new SQLException($error . '. Re-tried with error ' . $other_error_retries_done . ' times. Cannot reconnect as in transaction. Query: ' . $query); 
         } else { 
          $this->close_and_establish_new_database_connection(); 
          continue; // Try again 
         } 
        } 
        break; 

       default: 
        throw new SQLException($error . '. Query: ' . $query); 
        break; 
      } 
     } 

     // Check we have got a result 
     if (!$q instanceof mysqli_result) { 
      throw new SQLException('Seemed to have a result but it is not a mysqli_result object. Query: ' . $query); 
     } 

     // All worked ok, deal with the result 
     while (($row = $q->fetch_assoc())) { 
      $return[] = $row; 
     } 
     $end = microtime(1); 

     $this->debugData($start, $end, $query, 'DB', '', $total_time_start, $cache); 

     $this->last_affected_rows = $q->num_rows; 
     $q->free_result(); 
     break; 
    } 

    return $return; 
} 

显然它调用未包含在片段的一些功能,但你应该明白。

引发的异常是:

SQLException: Seemed to have a result but it is not a mysqli_result object. Query: SHOW STATUS LIKE "Questions" 

我会加入到我们的异常信息的东西输出什么$q实际上是等待它再次发生。

有没有其他人每个人都经历过这样的事情,你有什么建议吗?我非常感谢你的帮助。由于


编辑:

我们只是有它发生在一个非常简单的SELECT cols FROM table WHERE key = x LIMIT 1;类型的查询(未显示出于安全原因,真正的查询,但它是关于你可以有最简单的查询) 。这发生在我额外的日志记录已经生效之前。当我有希望获得更多细节时,我会再次更新。

+0

''我们没有得到'',所以只需显示'var_dump($ q)'得到了什么。 – xdazz 2014-09-05 09:43:09

+0

@xdazz是的,我已经添加了一些额外的日志记录,下次发生时,我会发布更多的实际返回的细节。 – 2014-09-05 10:03:41

回答

1

在你的代码,如果$qfalse,它仍然会进入的,如果条件:

if (!$q instanceof mysqli_result) { 

也许你需要else if代替。

+0

不,它不会。如果'$ q === false',那么它会碰到上面的部分,并且或者抛出一个更早的异常或'continue;'到另一个循环迭代中。 – 2014-09-05 10:02:21

+0

我的歉意,你说得对,'$ q'可能是假的。我期待switch中的'continue;'被'switch'忽略并继续'while'循环。实际上'继续;'在开关中与'break;'http:// stackoverflow相同。com/a/12349889/1178671,以便解释它。 'continue'应该是'continue 2;'。再次抱歉我上面的不正确评论。 – 2014-09-05 14:49:11