Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle the failover problem instead of return error connect to the next available DB #1064

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions runtime/lib/Propel.php
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ public static function getSlaveConnection($name)

$slaveconfigs = isset(self::$configuration['datasources'][$name]['slaves']) ? self::$configuration['datasources'][$name]['slaves'] : null;

if (empty($slaveconfigs)) {
if (empty($slaveconfigs) || empty($slaveconfigs['connection'])) {
// no slaves configured for this datasource
// fallback to the master connection
self::$connectionMap[$name]['slave'] = self::getMasterConnection($name);
Expand All @@ -625,9 +625,25 @@ public static function getSlaveConnection($name)
}
}

// initialize slave connection
$con = Propel::initConnection($conparams, $name);
self::$connectionMap[$name]['slave'] = $con;
try {
// initialize slave connection
$con = Propel::initConnection($conparams, $name);
self::$connectionMap[$name]['slave'] = $con;
} catch (\PropelException $e) {
if (strpos($e->getMessage(), "Unable to open PDO connection") !== 0) {
throw $e;
}
// if the slave connection have a problem remove this slave from the list
// connect to the next available slave or to the master
if (isset($slaveconfigs['connection']['dsn'])) {
self::$configuration->setParameter("datasources.$name.slaves", []);
} else {
unset($slaveconfigs['connection'][$randkey]);
self::$configuration->setParameter("datasources.$name.slaves.connection", $slaveconfigs['connection']);
}

return self::getSlaveConnection($name);
}
}
} // if datasource slave not set

Expand Down