merge zma_to_thread version of db functions, which include a mutex lock
This commit is contained in:
parent
865649d785
commit
b3d23ab233
|
@ -23,29 +23,35 @@
|
||||||
#include "zm.h"
|
#include "zm.h"
|
||||||
#include "zm_db.h"
|
#include "zm_db.h"
|
||||||
|
|
||||||
|
// From what I read, we need one of these per thread
|
||||||
MYSQL dbconn;
|
MYSQL dbconn;
|
||||||
|
Mutex db_mutex;
|
||||||
|
|
||||||
int zmDbConnected = false;
|
bool zmDbConnected = false;
|
||||||
|
|
||||||
void zmDbConnect() {
|
bool zmDbConnect() {
|
||||||
// For some reason having these lines causes memory corruption and crashing on newer debian/ubuntu
|
// For some reason having these lines causes memory corruption and crashing on newer debian/ubuntu
|
||||||
//if ( zmDbConnected )
|
//if ( zmDbConnected )
|
||||||
//return;
|
//return;
|
||||||
|
|
||||||
if ( !mysql_init( &dbconn ) ) {
|
if ( !mysql_init( &dbconn ) ) {
|
||||||
Error( "Can't initialise database connection: %s", mysql_error( &dbconn ) );
|
Error( "Can't initialise database connection: %s", mysql_error( &dbconn ) );
|
||||||
exit( mysql_errno( &dbconn ) );
|
return false;
|
||||||
}
|
}
|
||||||
my_bool reconnect = 1;
|
my_bool reconnect = 1;
|
||||||
if ( mysql_options( &dbconn, MYSQL_OPT_RECONNECT, &reconnect ) )
|
if ( mysql_options( &dbconn, MYSQL_OPT_RECONNECT, &reconnect ) )
|
||||||
Fatal( "Can't set database auto reconnect option: %s", mysql_error( &dbconn ) );
|
Error( "Can't set database auto reconnect option: %s", mysql_error( &dbconn) );
|
||||||
if ( !staticConfig.DB_SSL_CA_CERT.empty() )
|
if ( !staticConfig.DB_SSL_CA_CERT.empty() )
|
||||||
mysql_ssl_set( &dbconn, staticConfig.DB_SSL_CLIENT_KEY.c_str(), staticConfig.DB_SSL_CLIENT_CERT.c_str(), staticConfig.DB_SSL_CA_CERT.c_str(), NULL, NULL );
|
mysql_ssl_set( &dbconn,
|
||||||
|
staticConfig.DB_SSL_CLIENT_KEY.c_str(),
|
||||||
|
staticConfig.DB_SSL_CLIENT_CERT.c_str(),
|
||||||
|
staticConfig.DB_SSL_CA_CERT.c_str(),
|
||||||
|
NULL, NULL );
|
||||||
std::string::size_type colonIndex = staticConfig.DB_HOST.find( ":" );
|
std::string::size_type colonIndex = staticConfig.DB_HOST.find( ":" );
|
||||||
if ( colonIndex == std::string::npos ) {
|
if ( colonIndex == std::string::npos ) {
|
||||||
if ( !mysql_real_connect( &dbconn, staticConfig.DB_HOST.c_str(), staticConfig.DB_USER.c_str(), staticConfig.DB_PASS.c_str(), NULL, 0, NULL, 0 ) ) {
|
if ( !mysql_real_connect( &dbconn, staticConfig.DB_HOST.c_str(), staticConfig.DB_USER.c_str(), staticConfig.DB_PASS.c_str(), NULL, 0, NULL, 0 ) ) {
|
||||||
Error( "Can't connect to server: %s", mysql_error( &dbconn ) );
|
Error( "Can't connect to server: %s", mysql_error( &dbconn ) );
|
||||||
exit( mysql_errno( &dbconn ) );
|
return false;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
std::string dbHost = staticConfig.DB_HOST.substr( 0, colonIndex );
|
std::string dbHost = staticConfig.DB_HOST.substr( 0, colonIndex );
|
||||||
|
@ -53,12 +59,12 @@ void zmDbConnect() {
|
||||||
if ( dbPortOrSocket[0] == '/' ) {
|
if ( dbPortOrSocket[0] == '/' ) {
|
||||||
if ( !mysql_real_connect( &dbconn, NULL, staticConfig.DB_USER.c_str(), staticConfig.DB_PASS.c_str(), NULL, 0, dbPortOrSocket.c_str(), 0 ) ) {
|
if ( !mysql_real_connect( &dbconn, NULL, staticConfig.DB_USER.c_str(), staticConfig.DB_PASS.c_str(), NULL, 0, dbPortOrSocket.c_str(), 0 ) ) {
|
||||||
Error( "Can't connect to server: %s", mysql_error( &dbconn ) );
|
Error( "Can't connect to server: %s", mysql_error( &dbconn ) );
|
||||||
exit( mysql_errno( &dbconn ) );
|
return false;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if ( !mysql_real_connect( &dbconn, dbHost.c_str(), staticConfig.DB_USER.c_str(), staticConfig.DB_PASS.c_str(), NULL, atoi(dbPortOrSocket.c_str()), NULL, 0 ) ) {
|
if ( !mysql_real_connect( &dbconn, dbHost.c_str(), staticConfig.DB_USER.c_str(), staticConfig.DB_PASS.c_str(), NULL, atoi(dbPortOrSocket.c_str()), NULL, 0 ) ) {
|
||||||
Error( "Can't connect to server: %s", mysql_error( &dbconn ) );
|
Error( "Can't connect to server: %s", mysql_error( &dbconn ) );
|
||||||
exit( mysql_errno( &dbconn ) );
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -67,6 +73,7 @@ void zmDbConnect() {
|
||||||
exit( mysql_errno( &dbconn ) );
|
exit( mysql_errno( &dbconn ) );
|
||||||
}
|
}
|
||||||
zmDbConnected = true;
|
zmDbConnected = true;
|
||||||
|
return zmDbConnected;
|
||||||
}
|
}
|
||||||
|
|
||||||
void zmDbClose() {
|
void zmDbClose() {
|
||||||
|
@ -84,6 +91,7 @@ MYSQL_RES * zmDbFetch( const char * query ) {
|
||||||
Error( "Not connected." );
|
Error( "Not connected." );
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
db_mutex.lock();
|
||||||
|
|
||||||
if ( mysql_query( &dbconn, query ) ) {
|
if ( mysql_query( &dbconn, query ) ) {
|
||||||
Error( "Can't run query: %s", mysql_error( &dbconn ) );
|
Error( "Can't run query: %s", mysql_error( &dbconn ) );
|
||||||
|
@ -95,6 +103,7 @@ MYSQL_RES * zmDbFetch( const char * query ) {
|
||||||
Error( "Can't use query result: %s for query %s", mysql_error( &dbconn ), query );
|
Error( "Can't use query result: %s for query %s", mysql_error( &dbconn ), query );
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
db_mutex.unlock();
|
||||||
return result;
|
return result;
|
||||||
} // end MYSQL_RES * zmDbFetch( const char * query );
|
} // end MYSQL_RES * zmDbFetch( const char * query );
|
||||||
|
|
||||||
|
|
35
src/zm_db.h
35
src/zm_db.h
|
@ -21,37 +21,30 @@
|
||||||
#define ZM_DB_H
|
#define ZM_DB_H
|
||||||
|
|
||||||
#include <mysql/mysql.h>
|
#include <mysql/mysql.h>
|
||||||
|
#include "zm_thread.h"
|
||||||
|
|
||||||
class zmDbRow {
|
class zmDbRow {
|
||||||
private:
|
private:
|
||||||
MYSQL_RES *result_set;
|
MYSQL_RES *result_set;
|
||||||
MYSQL_ROW row;
|
MYSQL_ROW row;
|
||||||
public:
|
public:
|
||||||
zmDbRow() { result_set = NULL; row = NULL; };
|
zmDbRow() { result_set = NULL; row = NULL; };
|
||||||
MYSQL_RES *fetch( const char *query );
|
MYSQL_RES *fetch( const char *query );
|
||||||
zmDbRow( MYSQL_RES *, MYSQL_ROW *row );
|
zmDbRow( MYSQL_RES *, MYSQL_ROW *row );
|
||||||
~zmDbRow();
|
~zmDbRow();
|
||||||
|
|
||||||
char *operator[](unsigned int index) const {
|
char *operator[](unsigned int index) const {
|
||||||
return row[index];
|
return row[index];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
extern MYSQL dbconn;
|
extern MYSQL dbconn;
|
||||||
|
extern Mutex db_mutex;
|
||||||
|
|
||||||
extern int zmDbConnected;
|
bool zmDbConnect();
|
||||||
|
|
||||||
void zmDbConnect();
|
|
||||||
void zmDbClose();
|
void zmDbClose();
|
||||||
|
|
||||||
MYSQL_RES * zmDbFetch( const char *query );
|
MYSQL_RES * zmDbFetch( const char *query );
|
||||||
zmDbRow *zmDbFetchOne( const char *query );
|
zmDbRow *zmDbFetchOne( const char *query );
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
} /* extern "C" */
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#endif // ZM_DB_H
|
#endif // ZM_DB_H
|
||||||
|
|
Loading…
Reference in New Issue