From e8ba30d086bac8ed3f92b6928924d029cd5b4ac8 Mon Sep 17 00:00:00 2001 From: moneroexamples Date: Thu, 15 Dec 2016 14:57:21 +0800 Subject: [PATCH] Eatch thread has its own mysql connection now. --- src/MySqlConnector.h | 52 +++++++++++++++++++++----------------------- src/TxSearch.h | 31 +++++++++++++------------- 2 files changed, 40 insertions(+), 43 deletions(-) diff --git a/src/MySqlConnector.h b/src/MySqlConnector.h index b8153bb..de089ff 100644 --- a/src/MySqlConnector.h +++ b/src/MySqlConnector.h @@ -48,13 +48,6 @@ public: static string password; static string dbname; - static MySqlConnector& getInstance() - { - static MySqlConnector instance; // Guaranteed to be destroyed. - // Instantiated on first use. - return instance; - } -private: MySqlConnector() { if (conn.connected()) @@ -67,16 +60,6 @@ private: } } -public: - MySqlConnector(MySqlConnector const&) = delete; - void operator=(MySqlConnector const&) = delete; - -// Connection* -// get_connection() -// { -// return &conn; -// } - Query query(const char* qstr = 0) { @@ -105,8 +88,13 @@ string MySqlConnector::dbname; class MysqlTransactions { + shared_ptr conn; + public: + MysqlTransactions(shared_ptr _conn): conn {_conn} + {} + bool select(const uint64_t& address_id, vector& txs) { @@ -121,7 +109,7 @@ public: // query = shared_ptr(new Query(q)); // } - Query query = MySqlConnector::getInstance().query(XmrTransaction::SELECT_STMT); + Query query = conn->query(XmrTransaction::SELECT_STMT); query.parse(); try @@ -160,7 +148,7 @@ public: // } - Query query = MySqlConnector::getInstance().query(XmrTransaction::INSERT_STMT); + Query query = conn->query(XmrTransaction::INSERT_STMT); query.parse(); // cout << query << endl; @@ -199,11 +187,21 @@ public: class MySqlAccounts { - MysqlTransactions mysql_tx; + shared_ptr conn; + + shared_ptr mysql_tx; public: + MySqlAccounts() + { + cout << "MySqlAccounts() makes new connection" << endl; + conn = make_shared(); + mysql_tx = make_shared(conn); + } + + bool select(const string& address, XmrAccount& account) { @@ -217,7 +215,7 @@ public: // query = shared_ptr(new Query(q)); // } - Query query = MySqlConnector::getInstance().query(XmrAccount::SELECT_STMT); + Query query = conn->query(XmrAccount::SELECT_STMT); query.parse(); try @@ -257,7 +255,7 @@ public: // query = shared_ptr(new Query(q)); // } - Query query = MySqlConnector::getInstance().query(XmrAccount::SELECT_STMT2); + Query query = conn->query(XmrAccount::SELECT_STMT2); query.parse(); try @@ -294,7 +292,7 @@ public: // } - Query query = MySqlConnector::getInstance().query(XmrAccount::INSERT_STMT); + Query query = conn->query(XmrAccount::INSERT_STMT); query.parse(); // cout << query << endl; @@ -319,7 +317,7 @@ public: uint64_t insert_tx(const XmrTransaction& tx_data) { - return mysql_tx.insert(tx_data); + return mysql_tx->insert(tx_data); } bool @@ -339,13 +337,13 @@ public: } - return mysql_tx.select(acc.id, txs); + return mysql_tx->select(acc.id, txs); } bool select_txs(const uint64_t& account_id, vector& txs) { - return mysql_tx.select(account_id, txs); + return mysql_tx->select(account_id, txs); } @@ -353,7 +351,7 @@ public: update(XmrAccount& acc_orginal, XmrAccount& acc_new) { - Query query = MySqlConnector::getInstance().query(); + Query query = conn->query(); try { diff --git a/src/TxSearch.h b/src/TxSearch.h index ce39864..4fbf6b4 100644 --- a/src/TxSearch.h +++ b/src/TxSearch.h @@ -45,7 +45,7 @@ class TxSearch // its better to when each thread has its own mysql connection object. // this way if one thread crashes, it want take down // connection for the entire service - MySqlAccounts xmr_accounts; + shared_ptr xmr_accounts; // address and viewkey for this search thread. account_public_address address; @@ -53,13 +53,12 @@ class TxSearch public: - TxSearch() {} - TxSearch(XmrAccount& _acc): - acc {_acc}, - xmr_accounts() + acc {_acc} { + xmr_accounts = make_shared(); + bool testnet = CurrentBlockchainStatus::testnet; if (!xmreg::parse_str_address(acc.address, address, testnet)) @@ -125,13 +124,13 @@ public: } -// if (searched_blk_no % 10 == 0) -// { -// // print status every 10th block -// -// fmt::print(" - searching block {:d} of hash {:s} \n", -// searched_blk_no, pod_to_hex(get_block_hash(blk))); -// } + if (searched_blk_no % 100 == 0) + { + // print status every 10th block + + fmt::print(" - searching block {:d} of hash {:s} \n", + searched_blk_no, pod_to_hex(get_block_hash(blk))); + } for (transaction& tx: blk_txs) { @@ -271,7 +270,7 @@ public: tx_data.timestamp = XmrTransaction::timestamp_to_DateTime(blk.timestamp); // insert tx_data into mysql's Transactions table - uint64_t tx_mysql_id = xmr_accounts.insert_tx(tx_data); + uint64_t tx_mysql_id = xmr_accounts->insert_tx(tx_data); // once tx was added, update Accounts table @@ -280,7 +279,7 @@ public: updated_acc.total_received = acc.total_received + tx_data.total_received; - if (xmr_accounts.update(acc, updated_acc)) + if (xmr_accounts->update(acc, updated_acc)) { // iff success, set acc to updated_acc; acc = updated_acc; @@ -292,7 +291,7 @@ public: } // for (const transaction& tx: blk_txs) - if (searched_blk_no % 10) + if (searched_blk_no % 10 == 0) { // every 10 blocks updated scanned_block_height @@ -300,7 +299,7 @@ public: updated_acc.scanned_block_height = searched_blk_no; - if (xmr_accounts.update(acc, updated_acc)) + if (xmr_accounts->update(acc, updated_acc)) { // iff success, set acc to updated_acc; acc = updated_acc;