util: global lock for certain MySQL operations
Even the reentrant libmysqlclient_r has some non-threadsafe operations, which need to be protected by a global mutex. <http://dev.mysql.com/doc/refman/5.5/en/c-api-threaded-clients.html> Change-Id: I444e42f82cf982a6c8f8f2596e8991d0a5009b28
This commit is contained in:
@ -8,7 +8,15 @@ static fail::Logger LOG("Database", true);
|
|||||||
|
|
||||||
using namespace fail;
|
using namespace fail;
|
||||||
|
|
||||||
|
#ifndef __puma
|
||||||
|
boost::mutex Database::m_global_lock;
|
||||||
|
#endif
|
||||||
|
|
||||||
Database::Database(const std::string &username, const std::string &host, const std::string &database) {
|
Database::Database(const std::string &username, const std::string &host, const std::string &database) {
|
||||||
|
#ifndef __puma
|
||||||
|
boost::lock_guard<boost::mutex> guard(m_global_lock);
|
||||||
|
#endif
|
||||||
|
|
||||||
handle = mysql_init(0);
|
handle = mysql_init(0);
|
||||||
last_result = 0;
|
last_result = 0;
|
||||||
mysql_options(handle, MYSQL_READ_DEFAULT_FILE, "~/.my.cnf");
|
mysql_options(handle, MYSQL_READ_DEFAULT_FILE, "~/.my.cnf");
|
||||||
@ -26,6 +34,9 @@ Database::~Database()
|
|||||||
// flush cached INSERTs if available
|
// flush cached INSERTs if available
|
||||||
insert_multiple();
|
insert_multiple();
|
||||||
|
|
||||||
|
#ifndef __puma
|
||||||
|
boost::lock_guard<boost::mutex> guard(m_global_lock);
|
||||||
|
#endif
|
||||||
mysql_close(handle);
|
mysql_close(handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -23,6 +23,7 @@ namespace fail {
|
|||||||
MYSQL_RES *last_result; // !< Used for mysql_result_free
|
MYSQL_RES *last_result; // !< Used for mysql_result_free
|
||||||
#ifndef __puma
|
#ifndef __puma
|
||||||
boost::mutex m_handle_lock;
|
boost::mutex m_handle_lock;
|
||||||
|
static boost::mutex m_global_lock;
|
||||||
#endif
|
#endif
|
||||||
std::string m_insertquery;
|
std::string m_insertquery;
|
||||||
std::vector<std::string> m_insertquery_values;
|
std::vector<std::string> m_insertquery_values;
|
||||||
|
|||||||
Reference in New Issue
Block a user