Commit 1d8e8b6b authored by Tim Schoondergang's avatar Tim Schoondergang

Merge branch '4-catch-gone-away-and-treat-it-like-a-deadlock' into 'master'

Resolve "catch gone away and treat it like a deadlock"

Closes #4

See merge request !14
parents 60f5d6f9 f8e0a4b7
{
"name": "timmit/db-mysql",
"description": "A simple DB layer in PHP for using MySQL(i)",
"version": "v2.1.0",
"version": "v2.2.0",
"keywords": ["DB", "MySQL", "MySQLi", "database", "DB layer", "database layer"],
"license": "MIT",
"homepage": "http://www.TiMMiT.nl/",
......
......@@ -28,6 +28,27 @@ namespace TiMMiT\MySQL;
*/
public static $max_deadlock_count = 5;
/**
* Sleep multiplier with a deadlock
*
* @var int
*/
public static $sleep_deadlock_multiplier = 1;
/**
* Max tries before failure by gone away
*
* @var int
*/
public static $max_goneaway_count = 5;
/**
* Sleep multiplier with a deadlock
*
* @var float
*/
public static $sleep_goneaway_multiplier = 1.5;
/**
* The stats vars.
*
......@@ -97,7 +118,10 @@ namespace TiMMiT\MySQL;
* @return void
*/
public static function _disconnect() {
return mysqli_close(self::$mlink);
if(!self::$mlink){
return mysqli_close(self::$mlink);
}
return true;
}
/**
......@@ -124,14 +148,31 @@ namespace TiMMiT\MySQL;
}
if (!$result) {
$errorno = mysqli_errno(self::$mlink);
if ($errorno == 1213){
if($count < self::$max_deadlock_count){
self::query($query,$resultmode,($count+1));
} else {
trigger_error("Query Failed:<ul><li>errorno=" . mysqli_errno(self::$mlink) ."</li><li>failure_count=" . $count ." of max=" . self::$max_deadlock_count ." </li><li>error=" . mysqli_error(self::$mlink) . "</li><li>query=" . $query . "</li></ul>",E_USER_ERROR);
}
} else {
trigger_error("Query Failed:<ul><li>errorno=" . mysqli_errno(self::$mlink) ."</li><li>error=" . mysqli_error(self::$mlink) . "</li><li>query= " . $query . "</li></ul>",E_USER_ERROR);
switch($errorno){
case 1213:
if($count < self::$max_deadlock_count){
$count++;
usleep(($count*round(100000,1000000)*self::$sleep_deadlock_multiplier));
$result = self::query($query,$resultmode,($count+1));
} else {
trigger_error("Query Failed:<ul><li>errorno=" . $errorno ."</li><li>failure_count=" . $count ." of max=" . self::$max_deadlock_count ." </li><li>error=" . mysqli_error(self::$mlink) . "</li><li>query=" . $query . "</li></ul>",E_USER_ERROR);
}
break;
case 2006:
case 1047:
if($count < self::$max_goneaway_count){
mysqli_close(self::$mlink);
$count++;
usleep(($count*round(100000,1000000)*self::$sleep_goneaway_multiplier));
self::reconnect();
$result = self::query($query,$resultmode,$count);
} else {
trigger_error("Query Failed:<ul><li>errorno=" . $errorno ."</li><li>failure_count=" . $count ." of max=" . self::$max_goneaway_count ." </li><li>error=" . mysqli_error(self::$mlink) . "</li><li>query=" . $query . "</li></ul>",E_USER_ERROR);
}
break;
default:
trigger_error("Query Failed:<ul><li>errorno=" . $errorno ."</li><li>error=" . mysqli_error(self::$mlink) . "</li><li>query= " . $query . "</li></ul>",E_USER_ERROR);
break;
}
}
return $result;
......@@ -699,4 +740,4 @@ namespace TiMMiT\MySQL;
return self::$stats;
}
}
\ No newline at end of file
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment