Skip to content

Commit

Permalink
Added force reconnect to MySQL after query exception to fix Commands …
Browse files Browse the repository at this point in the history
…out of sync
  • Loading branch information
Markus Behm authored and italocoin committed Jan 10, 2019
1 parent 61c085c commit d34cf46
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 21 deletions.
29 changes: 16 additions & 13 deletions src/MySqlAccounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ MysqlInputs::select_for_out(const uint64_t& output_id, vector<XmrInput>& ins)
}
catch (std::exception const& e)
{
MYSQL_EXCEPTION_MSG(e);
MYSQL_EXCEPTION_MSG(e, conn);
//throw e;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand All @@ -207,7 +207,7 @@ MysqlPayments::select_by_payment_id(const string& payment_id, vector<XmrPayment>
}
catch (std::exception const& e)
{
MYSQL_EXCEPTION_MSG(e);
MYSQL_EXCEPTION_MSG(e, conn);
//throw e;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
Expand Down Expand Up @@ -319,7 +319,8 @@ MySqlAccounts::insert(const vector<T>& data_to_insert)
}
catch (std::exception const& e)
{
MYSQL_EXCEPTION_MSG(e);
MYSQL_EXCEPTION_MSG(e, conn);
//throw e;
}

return 0;
Expand Down Expand Up @@ -350,7 +351,8 @@ MySqlAccounts::select(uint64_t account_id, vector<T>& selected_data)
}
catch (std::exception const& e)
{
MYSQL_EXCEPTION_MSG(e);
MYSQL_EXCEPTION_MSG(e, conn);
//throw e;
}

return false;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/MySqlAccounts.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ class MySqlAccounts
}
catch (std::exception const& e)
{
MYSQL_EXCEPTION_MSG(e);
MYSQL_EXCEPTION_MSG(e, conn);
}

return 0;
Expand Down
6 changes: 3 additions & 3 deletions src/MySqlConnector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -48,7 +48,7 @@ MySqlConnector::connect()
}
catch (mysqlpp::ConnectionFailed const& e)
{
MYSQL_EXCEPTION_MSG(e);
MYSQL_EXCEPTION_MSG(e, ((MySqlConnector*)nullptr));
return false;
}

Expand Down
11 changes: 7 additions & 4 deletions src/MySqlConnector.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);


/*
Expand Down Expand Up @@ -63,7 +66,7 @@ class MySqlConnector
query(const std::string& qstr);

virtual bool
connect();
connect(const bool force_reconnect = false);

bool
ping();
Expand Down

0 comments on commit d34cf46

Please sign in to comment.