allow to specify multiple ;-separated hosts for database, by default first one is used, on connection failure other ones are tried too

This commit is contained in:
Ralf Becker 2015-07-06 19:38:27 +00:00
parent d0b0d688dc
commit 80691159f6
2 changed files with 66 additions and 12 deletions

View File

@ -1636,7 +1636,10 @@ class StreamWrapper implements Vfs\StreamWrapperIface
self::$pdo_type = $egw_db->Type;
break;
}
$dsn = self::$pdo_type.':dbname='.$egw_db->Database.($egw_db->Host ? ';host='.$egw_db->Host.($egw_db->Port ? ';port='.$egw_db->Port : '') : '');
// get host used be egw_db
$host = $egw_db->get_host();
$dsn = self::$pdo_type.':dbname='.$egw_db->Database.($host ? ';host='.$host.($egw_db->Port ? ';port='.$egw_db->Port : '') : '');
// check once if pdo extension and DB specific driver is loaded or can be loaded
static $pdo_available=null;
if (is_null($pdo_available))

View File

@ -298,16 +298,17 @@ class egw_db
}
/**
* Open a connection to a database
*
* @param string $Database name of database to use (optional)
* @param string $Host database host to connect to (optional)
* @param string $Port database port to connect to (optional)
* @param string $User name of database user (optional)
* @param string $Password password for database user (optional)
* @param string $Type type of database (optional)
* @return ADONewConnection
*/
* Open a connection to a database
*
* @param string $Database name of database to use (optional)
* @param string $Host database host to connect to (optional)
* @param string $Port database port to connect to (optional)
* @param string $User name of database user (optional)
* @param string $Password password for database user (optional)
* @param string $Type type of database (optional)
* @throws egw_exception_db_connection
* @return ADOConnection
*/
function connect($Database = NULL, $Host = NULL, $Port = NULL, $User = NULL, $Password = NULL,$Type = NULL)
{
/* Handle defaults */
@ -339,9 +340,59 @@ class egw_db
{
$this->Type = $GLOBALS['egw_info']['server']['db_type'];
}
// on connection failure re-try with an other host
// remembering in session which host we used last time
$use_host_from_session = true;
while(($host = $this->get_host(!$use_host_from_session)))
{
try {
//error_log(__METHOD__."() this->Host(s)=$this->Host, n=$n --> host=$host");
return $this->_connect($host);
}
catch(egw_exception_db_connection $e) {
_egw_log_exception($e);
$this->disconnect(); // force a new connect
$this->Type = $this->setupType; // get set to "mysql" for "mysqli"
$use_host_from_session = false; // re-try with next host from list
}
}
throw $e;
}
/**
* Get one of multiple (semicolon-separated) DB-hosts to use
*
* Which host to use is cached in session, default is first one.
*
* @param boolean $next =false true: move to next host
* @return boolean|string hostname or false, if already number-of-hosts plus 2 times called with $next == true
*/
public function get_host($next = false)
{
$hosts = explode(';', $this->Host);
$num_hosts = count($hosts);
$n =& egw_cache::getSession(__CLASS__, $this->Host);
if (!isset($n)) $n = 0;
if ($next && ++$n >= $num_hosts+2)
{
return false;
}
return $hosts[$n % $num_hosts];
}
/**
* Connect to given host
*
* @param string $Host host to connect to
* @return ADOConnection
* @throws egw_exception_db_connection
*/
protected function _connect($Host)
{
if (!$this->Link_ID)
{
foreach(array('Host','Database','User','Password') as $name)
foreach(array('Database','User','Password') as $name)
{
$$name = $this->$name;
}