[英]Missing “Lock wait timeout exceeded” handling in magento's pdo_mysql adapter?
如果我比較兩個Magento適配器類Varien_Db_Adapter_Mysqli和Varien_Db_Adapter_Pdo_Mysql,我可以在方法raw_query的查詢異常處理中找到一些差異。
<?php
class Varien_Db_Adapter_Mysqli extends Zend_Db_Adapter_Mysqli
{
//....
/**
* Run RAW Query
*
* @param string $sql
* @return Zend_Db_Statement_Interface
*/
public function raw_query($sql)
{
$timeoutMessage = 'SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction';
$tries = 0;
do {
$retry = false;
try {
$this->clear_result();
$result = $this->getConnection()->query($sql);
$this->clear_result();
} catch (Exception $e) {
if ($tries < 10 && $e->getMessage() == $timeoutMessage) {
$retry = true;
$tries++;
} else {
throw $e;
}
}
} while ($retry);
return $result;
}
//....
}
如果我在Varien_Db_Adapter_Pdo_Mysql中將它與相等的方法進行比較,我會發現另一個錯誤處理。 它不會檢查超時,但會檢查丟失的連接。
<?php
class Varien_Db_Adapter_Pdo_Mysql extends Zend_Db_Adapter_Pdo_Mysql implements Varien_Db_Adapter_Interface
{
//....
/**
* Run RAW Query
*
* @param string $sql
* @return Zend_Db_Statement_Interface
* @throws PDOException
*/
public function raw_query($sql)
{
$lostConnectionMessage = 'SQLSTATE[HY000]: General error: 2013 Lost connection to MySQL server during query';
$tries = 0;
do {
$retry = false;
try {
$result = $this->query($sql);
} catch (Exception $e) {
// Convert to PDOException to maintain backwards compatibility with usage of MySQL adapter
if ($e instanceof Zend_Db_Statement_Exception) {
$e = $e->getPrevious();
if (!($e instanceof PDOException)) {
$e = new PDOException($e->getMessage(), $e->getCode());
}
}
// Check to reconnect
if ($tries < 10 && $e->getMessage() == $lostConnectionMessage) {
$retry = true;
$tries++;
} else {
throw $e;
}
}
} while ($retry);
return $result;
}
//....
}
那是對的嗎? 檢查兩種失敗案例會不會更好?
例:
/**
* Run RAW Query
*
* @param string $sql
* @return Zend_Db_Statement_Interface
* @throws PDOException
*/
public function raw_query($sql)
{
$lostConnectionMessages = array(
'SQLSTATE[HY000]: General error: 2013 Lost connection to MySQL server during query',
'SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction',
);
$tries = 0;
do {
$retry = false;
try {
$result = $this->query($sql);
} catch (Exception $e) {
// Convert to PDOException to maintain backwards compatibility with usage of MySQL adapter
if ($e instanceof Zend_Db_Statement_Exception) {
$e = $e->getPrevious();
if (!($e instanceof PDOException)) {
$e = new PDOException($e->getMessage(), $e->getCode());
}
}
// Check to reconnect
if ($tries < 10 && in_array($e->getMessage(), $lostConnectionMessages)) {
$retry = true;
$tries++;
} else {
throw $e;
}
}
} while ($retry);
return $result;
}
我會說,是的,他們也應該檢查一下。 在Magento2中,他們甚至刪除了丟失連接重新連接 ,但在MysqlI適配器中它仍然存在。
這取決於(總是一個很好的答案:)
我認為丟失的連接應被視為系統設置中的錯誤。 Varien_Db_Adapter_Pdo_Mysql
工作方式隱藏了查詢時間慢的原因。 我寧願看到真正的例外,然后讓Magento嘗試自動重新建立連接。
鎖定等待超時也是如此。 我想知道這種超時是否發生。 通過自動重試屏蔽它們可能會使錯誤“消失”,但無法解決實際問題。
此類自動恢復代碼至少應該通過系統配置中的某個選項進行配置,例如“啟用數據庫查詢恢復模式”或類似的選項,默認為“禁用”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.