Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reimplement autoreconnect if sql server lost functionality
- Loading branch information
1 parent
41c0fc2
commit 409eda2
Showing
2 changed files
with
24 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,7 +116,7 @@ bool fuh::user_exists(const std::string& name) { | |
|
||
// Make a test query for this username | ||
try { | ||
return prepared_statement<bool>(conn, "SELECT 1 FROM `" + db_users_table_ + "` WHERE UPPER(username)=UPPER(?)", name); | ||
return prepared_statement<bool>("SELECT 1 FROM `" + db_users_table_ + "` WHERE UPPER(username)=UPPER(?)", name); | ||
} catch (sql_error& e) { | ||
ERR_UH << "Could not execute test query for user '" << name << "' :" << e.message << std::endl; | ||
// If the database is down just let all usernames log in | ||
|
@@ -246,17 +246,33 @@ void fuh::set_lastlogin(const std::string& user, const time_t& lastlogin) { | |
} | ||
} | ||
|
||
template<typename T, typename... Args> | ||
inline T fuh::prepared_statement(const std::string& sql, Args&&... args) | ||
{ | ||
try { | ||
return ::prepared_statement<T>(conn, sql, std::forward<Args>(args)...); | ||
} catch (sql_error&) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
AI0867
Member
|
||
WRN_UH << "caught sql error, trying to reconnect and retry..." << std::endl; | ||
//Try to reconnect and execute query again | ||
if(!mysql_real_connect(conn, db_host_.c_str(), db_user_.c_str(), db_password_.c_str(), db_name_.c_str(), 0, nullptr, 0)) { | ||
ERR_UH << "Could not connect to database: " << mysql_errno(conn) << ": " << mysql_error(conn) << std::endl; | ||
throw sql_error("Error querying database."); | ||
} | ||
} | ||
return ::prepared_statement<T>(conn, sql, std::forward<Args>(args)...); | ||
} | ||
|
||
template<typename T> | ||
T fuh::get_detail_for_user(const std::string& name, const std::string& detail) { | ||
return prepared_statement<T>(conn, | ||
return prepared_statement<T>( | ||
"SELECT `" + detail + "` FROM `" + db_users_table_ + "` WHERE UPPER(username)=UPPER(?)", | ||
name); | ||
} | ||
|
||
template<typename T> | ||
T fuh::get_writable_detail_for_user(const std::string& name, const std::string& detail) { | ||
if(!extra_row_exists(name)) throw sql_error("row doesn't exist"); | ||
return prepared_statement<T>(conn, | ||
return prepared_statement<T>( | ||
"SELECT `" + detail + "` FROM `" + db_extra_table_ + "` WHERE UPPER(username)=UPPER(?)", | ||
name); | ||
} | ||
|
@@ -267,9 +283,9 @@ void fuh::write_detail(const std::string& name, const std::string& detail, T&& v | |
// Check if we do already have a row for this user in the extra table | ||
if(!extra_row_exists(name)) { | ||
// If not create the row | ||
prepared_statement<void>(conn, "INSERT INTO `" + db_extra_table_ + "` VALUES(?,?,'0')", name, std::forward<T>(value)); | ||
prepared_statement<void>("INSERT INTO `" + db_extra_table_ + "` VALUES(?,?,'0')", name, std::forward<T>(value)); | ||
} | ||
prepared_statement<void>(conn, "UPDATE `" + db_extra_table_ + "` SET " + detail + "=? WHERE UPPER(username)=UPPER(?)", std::forward<T>(value), name); | ||
prepared_statement<void>("UPDATE `" + db_extra_table_ + "` SET " + detail + "=? WHERE UPPER(username)=UPPER(?)", std::forward<T>(value), name); | ||
} catch (sql_error& e) { | ||
ERR_UH << "Could not set detail for user '" << name << "': " << e.message << std::endl; | ||
} | ||
|
@@ -279,7 +295,7 @@ bool fuh::extra_row_exists(const std::string& name) { | |
|
||
// Make a test query for this username | ||
try { | ||
return prepared_statement<bool>(conn, "SELECT 1 FROM `" + db_extra_table_ + "` WHERE UPPER(username)=UPPER(?)", name); | ||
return prepared_statement<bool>("SELECT 1 FROM `" + db_extra_table_ + "` WHERE UPPER(username)=UPPER(?)", name); | ||
} catch (sql_error& e) { | ||
ERR_UH << "Could not execute test query for user '" << name << "' :" << e.message << std::endl; | ||
return false; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Do you want to catch and retry every sql_error? Most errors shouldn't really be a cause for reconnection, or is MySQL unreliable in that matter?
I'm also wondering if this couldn't cause a query to be executed twice. For most queries it won't matter (much), but for inserts this could be bad.
Finally, is it possible a correct query could throw depending on the parameters? In that case someone might be able to trigger a flood of reconnects.