libmysqlxx: changed settings to mysql [#CONV-8598]

This commit is contained in:
Pavel Kartavyy 2013-09-04 15:29:02 +00:00
parent 638799eee3
commit afa70827c7
2 changed files with 80 additions and 33 deletions

View File

@ -189,17 +189,29 @@ public:
*/
Pool(const std::string & config_name,
unsigned default_connections_ = MYSQLXX_POOL_DEFAULT_START_CONNECTIONS,
unsigned max_connections_ = MYSQLXX_POOL_DEFAULT_MAX_CONNECTIONS)
unsigned max_connections_ = MYSQLXX_POOL_DEFAULT_MAX_CONNECTIONS,
const std::string & user_default = "", const std::string & password_default = "",
const std::string & db_default = "", int port_default = 0)
: default_connections(default_connections_), max_connections(max_connections_),
initialized(false), was_successful(false)
{
Poco::Util::LayeredConfiguration & cfg = Poco::Util::Application::instance().config();
db = cfg.getString(config_name + ".db", "");
server = cfg.getString(config_name + ".host");
user = cfg.getString(config_name + ".user");
password = cfg.getString(config_name + ".password");
port = cfg.getInt(config_name + ".port");
db = cfg.getString(config_name + ".db", db_default);
if (!user_default.empty())
user = cfg.getString(config_name + ".user", user_default);
else
user = cfg.getString(config_name + ".user");
if (!password_default.empty())
password = cfg.getString(config_name + ".password", password_default);
else
password = cfg.getString(config_name + ".password");
if (port_default)
port = cfg.getInt(config_name + ".port", port_default);
else
port = cfg.getInt(config_name + ".port");
connect_timeout = cfg.getInt(config_name + ".connect_timeout",
cfg.getInt("mysql_connect_timeout",

View File

@ -10,7 +10,6 @@
namespace mysqlxx
{
/** Пул соединений с MySQL.
* Знает о наборе реплик с приоритетами.
* Пробует соединяться с репликами в порядке приоритета. При равном приоритете предпочитается реплика, к которой дольше всего не было попытки подключения.
@ -44,6 +43,23 @@ namespace mysqlxx
* <priority>1</priority>
* </replica>
* </mysql_metrica>
*
* или так:
*
* <mysql_metrica>
* <port>3306</port>
* <user>metrica</user>
* <password></password>
* <db>Metrica</db>
* <replica>
* <host>mtstat01c</host>
* <priority>0</priority>
* </replica>
* <replica>
* <host>mtstat01d</host>
* <priority>1</priority>
* </replica>
* </mysql_metrica>
*/
class PoolWithFailover
{
@ -90,17 +106,36 @@ namespace mysqlxx
Poco::Util::Application & app = Poco::Util::Application::instance();
Poco::Util::AbstractConfiguration & cfg = app.config();
if (cfg.has(config_name + ".replica"))
{
int port_g = 0;
std::string user_g;
std::string password_g;
std::string db_g;
if (cfg.has(config_name + ".user"))
user_g = cfg.getString(config_name + ".user");
if (cfg.has(config_name + ".password"))
password_g = cfg.getString(config_name + ".password");
if (cfg.has(config_name + ".db"))
db_g= cfg.getString(config_name + ".db");
if (cfg.has(config_name + ".port"))
port_g = cfg.getInt(config_name + ".port");
Poco::Util::AbstractConfiguration::Keys replica_keys;
cfg.keys(config_name, replica_keys);
for (Poco::Util::AbstractConfiguration::Keys::const_iterator it = replica_keys.begin(); it != replica_keys.end(); ++it)
{
if (it->size() < std::string("replica").size() || it->substr(0, std::string("replica").size()) != "replica")
throw Poco::Exception("Unknown element in config: " + *it + ", expected replica");
std::string replica_name = config_name + "." + *it;
Replica replica(new Pool(replica_name, default_connections, max_connections), cfg.getInt(replica_name + ".priority", 0));
replicas_by_priority[replica.priority].push_back(replica);
if (!(*it == "port" || *it == "ser" || *it == "password" || *it == "db"))
{
if (it->size() < std::string("replica").size() || it->substr(0, std::string("replica").size()) != "replica")
throw Poco::Exception("Unknown element in config: " + *it + ", expected replica");
std::string replica_name = config_name + "." + *it;
Replica replica(new Pool(replica_name, default_connections, max_connections, user_g, password_g, db_g, port_g),
cfg.getInt(replica_name + ".priority", 0));
replicas_by_priority[replica.priority].push_back(replica);
}
}
}
else