Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
MySQLBackend: disable autoreconnect, control CR_SERVER_LOST manually
* correctly recreate prepared statements
* do not close connection while reconnecting
* signed/unsigned comparison fix
  • Loading branch information
vitalyster committed Sep 21, 2018
1 parent 8780e3a commit be3af42
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
2 changes: 1 addition & 1 deletion include/transport/MySQLBackend.h
Expand Up @@ -129,7 +129,7 @@ class MySQLBackend : public StorageBackend
std::vector<MYSQL_BIND> m_params;
std::vector<MYSQL_BIND> m_results;
int m_error;
unsigned m_resultOffset;
int m_resultOffset;
unsigned m_offset;
std::string m_string;
};
Expand Down
12 changes: 5 additions & 7 deletions libtransport/MySQLBackend.cpp
Expand Up @@ -30,7 +30,7 @@
int ret = STMT->execute(); \
if (ret == 0) \
exec_ok = true; \
else if (ret == CR_SERVER_LOST) { \
else if (ret == CR_SERVER_LOST || ret == CR_SERVER_GONE_ERROR) { \
LOG4CXX_INFO(logger, "MySQL connection lost. Reconnecting...");\
disconnect(); \
connect(); \
Expand All @@ -52,7 +52,7 @@ MySQLBackend::Statement::Statement(MYSQL *conn, const std::string &format, const
m_string = statement;
m_stmt = mysql_stmt_init(conn);
if (mysql_stmt_prepare(m_stmt, statement.c_str(), statement.size())) {
LOG4CXX_ERROR(logger, statement << " " << mysql_error(conn));
LOG4CXX_ERROR(logger, statement << " " << mysql_stmt_error(m_stmt));
return;
}

Expand Down Expand Up @@ -126,14 +126,14 @@ MySQLBackend::Statement::Statement(MYSQL *conn, const std::string &format, const
}

if (mysql_stmt_bind_param(m_stmt, &m_params.front())) {
LOG4CXX_ERROR(logger, statement << " " << mysql_error(conn));
LOG4CXX_ERROR(logger, statement << " " << mysql_stmt_error(m_stmt));
}

if (m_resultOffset < 0)
m_resultOffset = format.size();
else {
if (mysql_stmt_bind_result(m_stmt, &m_results.front())) {
LOG4CXX_ERROR(logger, statement << " " << mysql_error(conn));
LOG4CXX_ERROR(logger, statement << " " << mysql_stmt_error(m_stmt));
}
}
m_resultOffset = 0;
Expand Down Expand Up @@ -227,12 +227,11 @@ MySQLBackend::MySQLBackend(Config *config) {
m_config = config;
m_prefix = CONFIG_STRING(m_config, "database.prefix");
mysql_init(&m_conn);
my_bool my_true = 1;
mysql_options(&m_conn, MYSQL_OPT_RECONNECT, &my_true);
}

MySQLBackend::~MySQLBackend(){
disconnect();
mysql_close(&m_conn);
}

void MySQLBackend::disconnect() {
Expand All @@ -257,7 +256,6 @@ void MySQLBackend::disconnect() {
delete m_setUserOnline;
delete m_getOnlineUsers;
delete m_getUsers;
mysql_close(&m_conn);
}

bool MySQLBackend::connect() {
Expand Down

0 comments on commit be3af42

Please sign in to comment.