public function connect($useMaster = false, $reconnect = false)
{
if ($reconnect) {
$this->disconnect($useMaster);
}
if ($useMaster) {
return $this->connectMaster($reconnect);
}
if (is_object($this->dbh) && !$reconnect) {
// Already connected
return 1;
}
static $retries = 0;
$connect_result = 1;
if ($this->debug > 1) {
echo 'DB CONNECT PARAMS: ' . print_r($this->connectParams, true) . "\n";
}
$dsn = $this->connectParams['type'] . ':dbname=' . $this->connectParams['db'] . ';host=' . $this->connectParams['host'];
if (isset($this->connectParams['sock'])) {
$dsn .= ';unix_socket=' . $this->connectParams['sock'];
}
try {
$this->dbh = new \PDO($dsn, $this->connectParams['username'], $this->connectParams['password'], $this->connectParams['attr']);
} catch (Exception $e) {
$this->errorMsg = $e->getMessage();
$this->errorCode = $e->getCode();
error_log('DB CLASS ERROR: ' . $this->errorMsg, 0);
if ($retries < 2) {
$retries++;
error_log('DB CLASS ERROR: Attempting to connect again - ' . $retries . ' ' . $this->connectParams['host'], 0);
usleep(100000);
$connect_result = $this->connect($useMaster, $reconnect);
} else {
$connect_result = -1;
$retries = 0;
}
return $connect_result;
}
//Update timeout setting
if ($this->timeout > 0) {
$this->rawQuery('SET SESSION wait_timeout=' . $this->timeout, false);
}
if ($reconnect) {
error_log('DB CLASS RECONNECTED (S)', 0);
}
self::$connections++;
if ($retries > 0) {
error_log('DB CLASS: Connected after ' . $retries . ' attempts ' . $this->connectParams['host']);
}
$retries = 0;
return $connect_result;
}