|
@@ -6,6 +6,15 @@ class Connection extends \Qii\Driver\ConnBase implements \Qii\Driver\ConnIntf
|
|
|
const VERSION = '1.2';
|
|
|
protected $_dbInfo;
|
|
|
|
|
|
+ /**
|
|
|
+ * @var resource $_writeConnection 获取写的链接
|
|
|
+ */
|
|
|
+ protected static $_writeConnection = null;
|
|
|
+ /**
|
|
|
+ * @var resource$_readConnection 获取读的链接
|
|
|
+ */
|
|
|
+ protected static $_readConnection = null;
|
|
|
+
|
|
|
public function __construct()
|
|
|
{
|
|
|
$this->_dbInfo = \Qii\Config\Register::getAppConfigure(\Qii\Config\Register::get(\Qii\Config\Consts::APP_DB));
|
|
@@ -16,6 +25,9 @@ class Connection extends \Qii\Driver\ConnBase implements \Qii\Driver\ConnIntf
|
|
|
*/
|
|
|
public function getReadConnection()
|
|
|
{
|
|
|
+ if (self::$_readConnection != null) {
|
|
|
+ return self::$_readConnection;
|
|
|
+ }
|
|
|
$dbInfo = $this->_dbInfo['master'];
|
|
|
$useSlave = false;
|
|
|
|
|
@@ -30,7 +42,7 @@ class Connection extends \Qii\Driver\ConnBase implements \Qii\Driver\ConnIntf
|
|
|
$connection = mysqli_connect($dbInfo['host'], $dbInfo['user'], $dbInfo['password'], $dbInfo['db']);
|
|
|
if (!$connection) throw new \Qii\Exceptions\Errors(\Qii::i(1501, iconv("GBK", "UTF-8//TRANSLIT", mysqli_connect_error())), true);
|
|
|
mysqli_select_db($connection, $dbInfo['db']);
|
|
|
- return $connection;
|
|
|
+ return self::$_readConnection = $connection;
|
|
|
} catch (Exception $e) {
|
|
|
return $this->getWriteConnection();
|
|
|
}
|
|
@@ -44,12 +56,15 @@ class Connection extends \Qii\Driver\ConnBase implements \Qii\Driver\ConnIntf
|
|
|
*/
|
|
|
public function getWriteConnection()
|
|
|
{
|
|
|
+ if (self::$_writeConnection != null) {
|
|
|
+ return self::$_writeConnection;
|
|
|
+ }
|
|
|
$dbInfo = $this->_dbInfo['master'];
|
|
|
try {
|
|
|
$connection = @mysqli_connect($dbInfo['host'], $dbInfo['user'], $dbInfo['password'], $dbInfo['db']);
|
|
|
if (!$connection) throw new \Qii\Exceptions\Errors(\Qii::i(1501, iconv("GBK", "UTF-8//TRANSLIT", mysqli_connect_error())), true);
|
|
|
mysqli_select_db($connection, $dbInfo['db']);
|
|
|
- return $connection;
|
|
|
+ return self::$_writeConnection = $connection;
|
|
|
} catch (Exception $e) {
|
|
|
throw new \Qii\Exceptions\Errors(\Qii::i(1500, $dbInfo['host'], $dbInfo['user'], $dbInfo['password'], $dbInfo['db'], toUTF8($e->getMessage())), __LINE__);
|
|
|
}
|