|
@@ -17,7 +17,7 @@ class Db
|
|
|
const ErrLock = 1205;
|
|
|
|
|
|
private static $link = array();
|
|
|
- private static $iftransacte = true;
|
|
|
+ private static $ifTransacting = false;
|
|
|
|
|
|
private function __construct()
|
|
|
{
|
|
@@ -31,10 +31,10 @@ class Db
|
|
|
|
|
|
private static function init_link()
|
|
|
{
|
|
|
- if(!isset(self::$link['master']) || is_object(self::$link['master']) == false) {
|
|
|
+ if(!isset(self::$link['master']) || (is_object(self::$link['master']) == false)) {
|
|
|
self::connect('master');
|
|
|
}
|
|
|
- if(!isset(self::$link['slave']) || is_object(self::$link['slave']) == false) {
|
|
|
+ if(!isset(self::$link['slave']) || (is_object(self::$link['slave']) == false)) {
|
|
|
self::connect('slave');
|
|
|
}
|
|
|
}
|
|
@@ -125,22 +125,27 @@ class Db
|
|
|
$eno = mysqli_errno(self::$link[$host]);
|
|
|
$emsg = mysqli_error(self::$link[$host]);
|
|
|
$error = "Db Error eno={$eno} msg={$emsg}";
|
|
|
-
|
|
|
Log::record("{$error} \r\n sql={$sql}",Log::ERR);
|
|
|
- if($eno == self::ErrUnConnect || $eno == self::ErrLock)
|
|
|
- {
|
|
|
- if($count > 0) return false;
|
|
|
- self::closeLink($host);
|
|
|
- self::connect($host);
|
|
|
- $count++;
|
|
|
+
|
|
|
+ if(self::$ifTransacting) {
|
|
|
+ throw_exception($error);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- if (C('debug')) {
|
|
|
- throw_exception($error.'<br/>'.$sql);
|
|
|
- return false;
|
|
|
- } else {
|
|
|
- return false;
|
|
|
+ if($eno == self::ErrUnConnect)
|
|
|
+ {
|
|
|
+ if($count > 0) return false;
|
|
|
+ self::connect($host);
|
|
|
+ $count++;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if (C('debug')) {
|
|
|
+ throw_exception($error.'<br/>'.$sql);
|
|
|
+ return false;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -611,46 +616,52 @@ class Db
|
|
|
public static function beginTransaction($host = 'master')
|
|
|
{
|
|
|
self::init_link();
|
|
|
- if (self::$iftransacte)
|
|
|
+ if (!self::$ifTransacting)
|
|
|
{
|
|
|
- Log::record("autocommit start 1",Log::DEBUG);
|
|
|
$result = self::$link[$host]->autocommit(false);
|
|
|
if($result == false)
|
|
|
{
|
|
|
- $errno = mysqli_errno(self::$link[$host]);
|
|
|
- $error = "Db Error autocommit no= error={$errno}" . mysqli_error(self::$link[$host]);
|
|
|
- Log::record($error."\r\n",Log::ERR);
|
|
|
+ self::connect($host);
|
|
|
+ self::$ifTransacting = false;
|
|
|
|
|
|
- if($errno == self::ErrUnConnect) {
|
|
|
- self::closeLink($host);
|
|
|
- self::connect($host);
|
|
|
- }
|
|
|
Log::record("autocommit start 2",Log::DEBUG);
|
|
|
self::$link[$host]->autocommit(false);
|
|
|
+ } else {
|
|
|
+ Log::record("autocommit start 1",Log::DEBUG);
|
|
|
}
|
|
|
}
|
|
|
- self::$iftransacte = false;
|
|
|
+ self::$ifTransacting = true;
|
|
|
}
|
|
|
|
|
|
public static function commit($host = 'master')
|
|
|
{
|
|
|
- if (!self::$iftransacte) {
|
|
|
+ if (self::$ifTransacting)
|
|
|
+ {
|
|
|
$result = self::$link[$host]->commit();
|
|
|
- Log::record("autocommit end 1",Log::DEBUG);
|
|
|
- self::$link[$host]->autocommit(true);//开启自动提交
|
|
|
- self::$iftransacte = true;
|
|
|
- if (!$result) throw_exception("Db Error: ".mysqli_error(self::$link[$host]));
|
|
|
+ Log::record("autocommit end commit",Log::DEBUG);
|
|
|
+ self::$link[$host]->autocommit(true);
|
|
|
+ self::$ifTransacting = false;
|
|
|
+
|
|
|
+ if (!$result) {
|
|
|
+ $err = mysqli_error(self::$link[$host]);
|
|
|
+ throw_exception("Db Error: {$err}");
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public static function rollback($host = 'master')
|
|
|
{
|
|
|
- if (!self::$iftransacte){
|
|
|
+ if (self::$ifTransacting){
|
|
|
$result = self::$link[$host]->rollback();
|
|
|
- Log::record("autocommit end 2",Log::DEBUG);
|
|
|
- self::$link[$host]->autocommit(true);
|
|
|
- self::$iftransacte = true;
|
|
|
- if (!$result) throw_exception("Db Error: ".mysqli_error(self::$link[$host]));
|
|
|
+ Log::record("autocommit end rollback",Log::DEBUG);
|
|
|
+ $fsuccess = self::$link[$host]->autocommit(true);
|
|
|
+ self::$ifTransacting = false;
|
|
|
+
|
|
|
+ if (!$result || !$fsuccess) {
|
|
|
+ $err = mysqli_error(self::$link[$host]);
|
|
|
+ self::connect('master');
|
|
|
+ throw_exception("Db Error: {$err}");
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|