catalog: add connection pool for the database
Change-Id: Ie014a5b11081908ea1cfdf30451ef44757d37c7e
diff --git a/catalog/src/util/mysql-util.cpp b/catalog/src/util/mysql-util.cpp
index 9df8b8e..38c0f3a 100644
--- a/catalog/src/util/mysql-util.cpp
+++ b/catalog/src/util/mysql-util.cpp
@@ -31,55 +31,26 @@
// empty
}
-
-std::shared_ptr<MYSQL>
-MySQLConnectionSetup(const ConnectionDetails& details)
+std::shared_ptr<ConnectionPool_T>
+zdbConnectionSetup(const ConnectionDetails& details)
{
- MYSQL* conn = mysql_init(NULL);
- my_bool reconnect = 1;
- mysql_options(conn, MYSQL_OPT_RECONNECT, &reconnect);
- if(!mysql_real_connect(conn, details.server.c_str(), details.user.c_str(),
- details.password.c_str(), details.database.c_str(), 0, NULL, 0)) {
- throw std::runtime_error(mysql_error(conn));
- }
- std::shared_ptr<MYSQL> connection(conn, &mysql_close);
- return connection;
-}
+ std::string dbConnStr("mysql://");
+ dbConnStr += details.user;
+ dbConnStr += ":";
+ dbConnStr += details.password;
+ dbConnStr += "@";
+ dbConnStr += details.server;
+ dbConnStr += ":3306/";
+ dbConnStr += details.database;
-std::shared_ptr<MYSQL_RES>
-MySQLPerformQuery(std::shared_ptr<MYSQL> connection,
- const std::string& sql_query,
- DatabaseOperation op,
- bool& success,
- std::string& errMsg)
-{
- switch (mysql_query(connection.get(), sql_query.c_str()))
- {
- case 0:
- {
- success = true;
- if (op == QUERY) {
- MYSQL_RES* resultPtr = mysql_store_result(connection.get());
- if (resultPtr != NULL)
- {
- return std::shared_ptr<MYSQL_RES>(resultPtr, &mysql_free_result);
- }
- }
- //for add, remove, we don't need the results, may be log the events
- break;
- }
- // Various error cases
- case CR_COMMANDS_OUT_OF_SYNC:
- case CR_SERVER_GONE_ERROR:
- case CR_SERVER_LOST:
- case CR_UNKNOWN_ERROR:
- default:
- {
- errMsg.assign(mysql_error(connection.get()));
- break;
- }
- }
- return nullptr;
+ URL_T url = URL_new(dbConnStr.c_str());
+
+ ConnectionPool_T dbConnPool = ConnectionPool_new(url);
+ ConnectionPool_setMaxConnections(dbConnPool, MAX_DB_CONNECTIONS);
+ ConnectionPool_setReaper(dbConnPool, 1);
+ ConnectionPool_start(dbConnPool);
+ auto sharedPool = std::make_shared<ConnectionPool_T>(dbConnPool);
+ return sharedPool;
}
} // namespace util