Browse Source

Add close db proccess in every half hour

Wan Xin 9 years ago
parent
commit
e49fed16a7
1 changed files with 14 additions and 1 deletions
  1. 14 1
      core/framework/db/mysqli.php

+ 14 - 1
core/framework/db/mysqli.php

@@ -14,6 +14,8 @@ class Db{
 
 	private static $iftransacte = true;
 
+	private static $connect_time = 0;
+
 	private function __construct(){
 		if (!extension_loaded('mysqli')){
 			throw_exception("Db Error: mysqli is not install");
@@ -21,6 +23,9 @@ class Db{
 	}
 
 	private static function connect($host = 'slave'){
+		if((time() - self::$connect_time > 1800) && defined('MOBILE_SERVER') && self::$connect_time > 0){
+			self::closeLink();
+		}
 		if (C('db.master') == C('db.slave')){
 			if (is_object(self::$link['slave'])){
 				self::$link['master'] = & self::$link['slave'];return ;
@@ -32,7 +37,7 @@ class Db{
 		$conf = C('db.'.$host);
 		if (is_object(self::$link[$host])) return;
 		self::$link[$host] = @new mysqli($conf['dbhost'], $conf['dbuser'], $conf['dbpwd'], $conf['dbname'], $conf['dbport']);
-
+		self::$connect_time = time();
 		if (mysqli_connect_errno()) throw_exception("Db Error: database connect failed");
 
 		switch (strtoupper($conf['dbcharset'])){
@@ -70,6 +75,14 @@ class Db{
 		}
 	}
 
+	private static function closeLink(){
+		foreach(self::$link as $db){
+			if(is_object($db)){
+				mysqli_close($db);
+			}
+		}
+	}
+
     public static function ping($host = 'master') {
         if (is_object(self::$link[$host])) {
 //         if (is_object(self::$link[$host]) && !self::$link[$host]->ping()) {