Index: db_connection.php =================================================================== --- db_connection.php (revision 14234) +++ db_connection.php (working copy) @@ -27,7 +27,6 @@ */ var $Application; - /** * Current database type * @@ -45,6 +44,13 @@ var $connectionID = null; /** + * Connection parameters, that were used + * + * @var Array + */ + var $connectionParams = Array ('host' => '', 'user' => '', 'pass' => '', 'db' => ''); + + /** * Handle of currenty processed recordset * * @var resource @@ -230,8 +236,10 @@ * @param string $db * @access public */ - function Connect($host, $user, $pass, $db, $force_new = false) + function Connect($host, $user, $pass, $db, $force_new = false, $retry = false) { + $this->connectionParams = Array ('host' => $host, 'user' => $user, 'pass' => $pass, 'db' => $db); + $func = $this->getMetaFunction('connect'); $this->connectionID = $func($host, $user, $pass, $force_new); @@ -244,6 +252,7 @@ $this->Query('SET NAMES \''.SQL_CHARSET.'\' COLLATE \''.SQL_COLLATION.'\''); } + $this->setError(0, ''); // reset error $this->setDB($db); } @@ -252,21 +261,44 @@ $this->errorCode = $this->connectionID ? $func($this->connectionID) : $func(); if ( !$this->hasError() ) { - return ; + return true; } $func = $this->getMetaFunction('error'); $this->errorMessage = $this->connectionID ? $func($this->connectionID) : $func(); $error_msg = 'Database connection failed, please check your connection settings.
Error (' . $this->errorCode . '): ' . $this->errorMessage; - - trigger_error($error_msg, defined('IS_INSTALL') && IS_INSTALL ? E_USER_WARNING : E_USER_ERROR); + + trigger_error($error_msg, (defined('IS_INSTALL') && IS_INSTALL) || $retry ? E_USER_WARNING : E_USER_ERROR); + + return false; } - function ReConnect($host, $user, $pass, $db, $force_new = false) + function ReConnect($force_new = false) { + $retry_count = 0; + $func = $this->getMetaFunction('close'); $func($this->connectionID); - $this->Connect($host, $user, $pass, $db, $force_new); + + while ($retry_count < 3) { + sleep(5); // wait 5 seconds before each reconnect attempt + + $connected = $this->Connect( + $this->connectionParams['host'], + $this->connectionParams['user'], + $this->connectionParams['pass'], + $this->connectionParams['db'], + $force_new, true + ); + + if ($connected) { + break; + } + + $retry_count++; + } + + return $connected; } /** @@ -275,36 +307,74 @@ * * @access private */ - function showError($sql = '') + function showError($sql = '', $key_field = null, $no_debug = false) { - $this->setError(0, ''); // reset error + static $retry_count = 0; + $func = $this->getMetaFunction('errno'); + if (!$this->connectionID) { - return ; + // no connection while doing mysql_query + $this->errorCode = $func(); + + if ( $this->hasError() ) { + $func = $this->getMetaFunction('error'); + $this->errorMessage = $func(); + + $ret = $this->callErrorHandler($sql); + + if (!$ret) { + exit; + } + } + + return false; } - $func = $this->getMetaFunction('errno'); + // checking if there was an error during last mysql_query $this->errorCode = $func($this->connectionID); - if ($this->hasError()) { + if ( $this->hasError() ) { $func = $this->getMetaFunction('error'); $this->errorMessage = $func($this->connectionID); - if (is_array($this->errorHandler)) { - $func = $this->errorHandler[1]; - $ret = $this->errorHandler[0]->$func($this->errorCode, $this->errorMessage, $sql); + $ret = $this->callErrorHandler($sql); + + if ( ($this->errorCode == 2006 || $this->errorCode == 2013) && ($retry_count < 3) ) { + // #2006 - MySQL server has gone away + // #2013 - Lost connection to MySQL server during query + $retry_count++; + + if ( $this->ReConnect() ) { + return $this->Query($sql, $key_field, $no_debug); + } } - else { - $func = $this->errorHandler; - $ret = $func($this->errorCode, $this->errorMessage, $sql); - } if (!$ret) { exit; } } + else { + $retry_count = 0; + } + + return false; } + function callErrorHandler($sql) + { + if (is_array($this->errorHandler)) { + $func = $this->errorHandler[1]; + $ret = $this->errorHandler[0]->$func($this->errorCode, $this->errorMessage, $sql); + } + else { + $func = $this->errorHandler; + $ret = $func($this->errorCode, $this->errorMessage, $sql); + } + + return $ret; + } + /** * Default error handler for sql errors * @@ -359,7 +429,7 @@ * Returns first row of recordset * if query ok, false otherwise * - * @param stirng $sql + * @param string $sql * @param int $offset * @return Array * @access public @@ -436,6 +506,7 @@ } // set 1st checkpoint: end + $this->setError(0, ''); // reset error $this->queryID = $query_func($sql,$this->connectionID); if (is_resource($this->queryID)) { $ret = Array(); @@ -471,8 +542,8 @@ } // set 2nd checkpoint: end } - $this->showError($sql); - return false; + + return $this->showError($sql, $key_field, $no_debug); } function ChangeQuery($sql) @@ -494,6 +565,7 @@ } // set 1st checkpoint: end + $this->setError(0, ''); // reset error $this->queryID = $query_func($sql, $this->connectionID); if( is_resource($this->queryID) ) @@ -541,8 +613,8 @@ } // set 2nd checkpoint: end } - $this->showError($sql); - return false; + + return $this->showError($sql, $key_field); } /**