Driver_Database_Driver_Mongo::_connect
null Driver_Database_Driver_Mongo::_connect( )
File: ./drivers/database/mongo/mongo.class.php
protected function _connect()
{
$database = $hostname = $port = $username = $password = $persistent = $readpreference = null;
extract($this->config['connection']);
if (!$port>0)
{
$port = 27017;
}
# 检查下是否已经有连接连上去了
if (Database_Driver_Mongo::$_connection_instance)
{
if (is_array($hostname))
{
$hostconfig = $hostname[$this->_connection_type];
if (!$hostconfig)
{
throw new Exception('指定的数据库连接主从配置中('.$this->_connection_type.')不存在,请检查配置');
}
if (!is_array($hostconfig))
{
$hostconfig = array($hostconfig);
}
}
else
{
$hostconfig = array
(
$hostname
);
}
# 先检查是否已经有相同的连接连上了数据库
foreach ($hostconfig as $host)
{
$_connection_id = $this->_get_connection_hash($host, $port, $username);
if (isset(Database_Driver_Mongo::$_connection_instance[$_connection_id]))
{
$this->_connection_ids[$this->_connection_type] = $_connection_id;
return;
}
}
}
// 获取所有的主数据库
if (is_array($this->config['connection']['hostname']))
{
if (is_array($this->config['connection']['hostname']['master']))
{
$all_master_hosts = $this->config['connection']['hostname']['master'];
}
else
{
$all_master_hosts = array
(
$this->config['connection']['hostname']['master']
);
}
}
else
{
$all_master_hosts = array
(
$this->config['connection']['hostname']
);
}
# 错误服务器
static $error_host = array();
$last_error = null;
while (true)
{
$hostname = $this->_get_rand_host($error_host);
if (false===$hostname)
{
Core::debug()->error($error_host,'error_host');
if ($last_error)throw $last_error;
throw new Exception('connect mongodb server error.');
}
$_connection_id = $this->_get_connection_hash($hostname, $port, $username);
Database_Driver_Mongo::$_current_connection_id_to_hostname[$_connection_id] = $hostname.':'.$port;
try
{
$time = microtime(true);
$options = array();
// 所有非主数据库都加上replicaSet参数
if (!in_array($hostname, $all_master_hosts))
{
$options['replicaSet'] = true;
}
// 长连接设计
if ($persistent)
{
$options['persist'] = is_string($persistent)?$persistent:'x';
}
static $mclass = null;
if (null===$mclass)
{
$mclass = class_exists('MongoClient', false)?'MongoClient':'Mongo';
}
$error_code = 0;
$error_msg = '';
try
{
if ($username)
{
$tmplink = new $mclass("mongodb://{$username}:{$password}@{$hostname}:{$port}/", $options);
}
else
{
$tmplink = new $mclass("mongodb://{$hostname}:{$port}/", $options);
}
}
catch (Exception $e)
{
$error_msg = $e->getMessage();
$error_code = $e->getCode();
$tmplink = false;
}
if (false===$tmplink)
{
if (IS_DEBUG)
{
throw $e;
}
else
{
$error_msg = 'connect mongodb server error.';
}
throw new Exception($error_msg, $error_code);
}
if (null!==$readpreference)
{
$tmplink->setReadPreference($readpreference);
}
Core::debug()->info('MongoDB '.($username?$username.'@':'').$hostname.':'.$port.' connection time:' . (microtime(true) - $time));
# 连接ID
$this->_connection_ids[$this->_connection_type] = $_connection_id;
Database_Driver_Mongo::$_connection_instance[$_connection_id] = $tmplink;
unset($tmplink);
break;
}
catch (Exception $e)
{
if (IS_DEBUG)
{
Core::debug()->error(($username?$username.'@':'').$hostname.':'.$port, 'connect mongodb server error');
$last_error = new Exception($e->getMessage(),$e->getCode());
}
else
{
$last_error = new Exception('connect mongodb server error', $e->getCode());
}
if (!in_array($hostname, $error_host))
{
$error_host[] = $hostname;
}
}
}
}