Commit 10efbb0e authored by Carla Guillen's avatar Carla Guillen
Browse files

Call once to initialize MariaDB.

parent 7fe40599
......@@ -63,6 +63,7 @@ public:
MariaDB * MariaDB::instance = nullptr;
std::mutex MariaDB::mut;
std::once_flag MariaDB::init_once;
void MariaDB::print_error(){
LOG(error) << "Error(" << mysql_errno(_mysql) << ") [" << mysql_sqlstate(_mysql) << "] \""<< mysql_error(_mysql) << "\"" ;
......@@ -75,14 +76,13 @@ void MariaDB::print_error(){
MariaDB::MariaDB(): _mysql(NULL), _rotation(EVERY_MONTH), _every_x_days(0), _end_aggregate_timestamp(0), _initialized(false) {
}
MariaDB * MariaDB::getInstance(){
// no lock here
if (instance) return instance;
std::lock_guard<std::mutex> lock(mut);
if (instance) return instance;
void MariaDB::initialize(){
instance = new MariaDB();
}
return instance = new MariaDB();
MariaDB * MariaDB::getInstance(){
std::call_once(init_once, &MariaDB::initialize);
return instance;
}
bool MariaDB::initializeConnection(const std::string & host, const std::string & user,
......
......@@ -62,6 +62,7 @@ protected:
MariaDB();
virtual ~MariaDB();
static void initialize();
MYSQL *_mysql;
Rotation_t _rotation;
......@@ -72,6 +73,7 @@ protected:
static MariaDB * instance;
static std::mutex mut;
static std::once_flag init_once;
bool _initialized;
std::map<std::string, Job_info_t> _jobCache;
const std::size_t JOB_CACHE_MAX_SIZE = 10000;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment