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;
|
||||
|
||||
#ifndef __puma
|
||||
boost::mutex Database::m_global_lock;
|
||||
#endif
|
||||
|
||||
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);
|
||||
last_result = 0;
|
||||
mysql_options(handle, MYSQL_READ_DEFAULT_FILE, "~/.my.cnf");
|
||||
@ -26,6 +34,9 @@ Database::~Database()
|
||||
// flush cached INSERTs if available
|
||||
insert_multiple();
|
||||
|
||||
#ifndef __puma
|
||||
boost::lock_guard<boost::mutex> guard(m_global_lock);
|
||||
#endif
|
||||
mysql_close(handle);
|
||||
}
|
||||
|
||||
|
||||
@ -23,6 +23,7 @@ namespace fail {
|
||||
MYSQL_RES *last_result; // !< Used for mysql_result_free
|
||||
#ifndef __puma
|
||||
boost::mutex m_handle_lock;
|
||||
static boost::mutex m_global_lock;
|
||||
#endif
|
||||
std::string m_insertquery;
|
||||
std::vector<std::string> m_insertquery_values;
|
||||
|
||||
Reference in New Issue
Block a user