From d34cf46abbf59feec7415aa17bcaa94797973b92 Mon Sep 17 00:00:00 2001 From: Markus Behm Date: Mon, 10 Dec 2018 13:01:13 +0000 Subject: [PATCH] Added force reconnect to MySQL after query exception to fix Commands out of sync --- src/MySqlAccounts.cpp | 29 ++++++++++++++++------------- src/MySqlAccounts.h | 2 +- src/MySqlConnector.cpp | 6 +++--- src/MySqlConnector.h | 11 +++++++---- 4 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/MySqlAccounts.cpp b/src/MySqlAccounts.cpp index 2b82941b3..cbc0e21f8 100755 --- a/src/MySqlAccounts.cpp +++ b/src/MySqlAccounts.cpp @@ -34,7 +34,7 @@ MysqlInputs::select_for_out(const uint64_t& output_id, vector& ins) } catch (std::exception const& e) { - MYSQL_EXCEPTION_MSG(e); + MYSQL_EXCEPTION_MSG(e, conn); //throw e; } @@ -69,7 +69,7 @@ MysqlOutpus::exist(const string& output_public_key_str, XmrOutput& out) } catch (std::exception const& e) { - MYSQL_EXCEPTION_MSG(e); + MYSQL_EXCEPTION_MSG(e, conn); return false; } @@ -99,7 +99,7 @@ MysqlTransactions::mark_spendable(const uint64_t& tx_id_no, bool spendable) } catch (std::exception const& e) { - MYSQL_EXCEPTION_MSG(e); + MYSQL_EXCEPTION_MSG(e, conn); //throw e; } @@ -122,7 +122,7 @@ MysqlTransactions::delete_tx(const uint64_t& tx_id_no) } catch (std::exception const& e) { - MYSQL_EXCEPTION_MSG(e); + MYSQL_EXCEPTION_MSG(e, conn); //throw e; } @@ -152,7 +152,7 @@ MysqlTransactions::exist(const uint64_t& account_id, const string& tx_hash_str, } catch (std::exception const& e) { - MYSQL_EXCEPTION_MSG(e); + MYSQL_EXCEPTION_MSG(e, conn); return false; } @@ -180,7 +180,7 @@ MysqlTransactions::get_total_recieved(const uint64_t& account_id, uint64_t& amou } catch (std::exception const& e) { - MYSQL_EXCEPTION_MSG(e); + MYSQL_EXCEPTION_MSG(e, conn); } return false; @@ -207,7 +207,7 @@ MysqlPayments::select_by_payment_id(const string& payment_id, vector } catch (std::exception const& e) { - MYSQL_EXCEPTION_MSG(e); + MYSQL_EXCEPTION_MSG(e, conn); //throw e; } @@ -255,7 +255,7 @@ MySqlAccounts::select(const string& address, XmrAccount& account) } catch (std::exception const& e) { - MYSQL_EXCEPTION_MSG(e); + MYSQL_EXCEPTION_MSG(e, conn); //throw e; } @@ -282,7 +282,7 @@ MySqlAccounts::insert(const T& data_to_insert) } catch (std::exception const& e) { - MYSQL_EXCEPTION_MSG(e);; + MYSQL_EXCEPTION_MSG(e, conn);; } return 0; @@ -319,7 +319,8 @@ MySqlAccounts::insert(const vector& data_to_insert) } catch (std::exception const& e) { - MYSQL_EXCEPTION_MSG(e); + MYSQL_EXCEPTION_MSG(e, conn); + //throw e; } return 0; @@ -350,7 +351,8 @@ MySqlAccounts::select(uint64_t account_id, vector& selected_data) } catch (std::exception const& e) { - MYSQL_EXCEPTION_MSG(e); + MYSQL_EXCEPTION_MSG(e, conn); + //throw e; } return false; @@ -396,7 +398,8 @@ MySqlAccounts::update(T const& orginal_row, T const& new_row) } catch (std::exception const& e) { - MYSQL_EXCEPTION_MSG(e); + MYSQL_EXCEPTION_MSG(e, conn); + //throw e; } return false; @@ -445,7 +448,7 @@ MySqlAccounts::select_by_primary_id(uint64_t id, T& selected_data) } catch (std::exception const& e) { - MYSQL_EXCEPTION_MSG(e); + MYSQL_EXCEPTION_MSG(e, conn); //throw e; } diff --git a/src/MySqlAccounts.h b/src/MySqlAccounts.h index 5a4840557..d400f15b8 100755 --- a/src/MySqlAccounts.h +++ b/src/MySqlAccounts.h @@ -228,7 +228,7 @@ class MySqlAccounts } catch (std::exception const& e) { - MYSQL_EXCEPTION_MSG(e); + MYSQL_EXCEPTION_MSG(e, conn); } return 0; diff --git a/src/MySqlConnector.cpp b/src/MySqlConnector.cpp index d8fd5cf17..b00cd443d 100755 --- a/src/MySqlConnector.cpp +++ b/src/MySqlConnector.cpp @@ -33,9 +33,9 @@ MySqlConnector::MySqlConnector(Option* _option) } bool -MySqlConnector::connect() +MySqlConnector::connect(const bool force_reconnect) { - if (conn.connected()) + if (!force_reconnect && conn.connected()) return true; try @@ -48,7 +48,7 @@ MySqlConnector::connect() } catch (mysqlpp::ConnectionFailed const& e) { - MYSQL_EXCEPTION_MSG(e); + MYSQL_EXCEPTION_MSG(e, ((MySqlConnector*)nullptr)); return false; } diff --git a/src/MySqlConnector.h b/src/MySqlConnector.h index f5dc39f1a..28515b456 100755 --- a/src/MySqlConnector.h +++ b/src/MySqlConnector.h @@ -19,10 +19,13 @@ namespace xmreg using namespace mysqlpp; using namespace std; -#define MYSQL_EXCEPTION_MSG(sql_excetption) cerr << "# ERR: SQLException in " << __FILE__ \ +#define MYSQL_EXCEPTION_MSG(sql_except, sql_conn) do { \ + cerr << "# ERR: SQLException in " << __FILE__ \ << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl \ - << "# ERR: " << sql_excetption.what() \ - << endl; + << "# ERR: " << sql_except.what() \ + << endl; \ + if (sql_conn && !sql_conn->connect(true)) throw std::runtime_error("Failed reconnecting to MySQL after exception"); \ +} while (false); /* @@ -63,7 +66,7 @@ class MySqlConnector query(const std::string& qstr); virtual bool - connect(); + connect(const bool force_reconnect = false); bool ping();