diff --git a/lib/mysql.hpp b/lib/mysql.hpp index dc07a36..0c5bf46 100644 --- a/lib/mysql.hpp +++ b/lib/mysql.hpp @@ -103,12 +103,10 @@ class MySQL { queue connection_pool; string path, username, password, database; uint32_t pool_size; - uint32_t connect_trys = 3; bool run_tloop = true; future tloop_future; time_loop_type tloop_type; - time_t last_loop_time = time(nullptr); - function on_error; + time_t last_loop_time; /** * Open one database server connection @@ -158,6 +156,9 @@ class MySQL { void _tloop(); public: + function on_error; + uint32_t connect_trys = 3; + /** * MySQL constructor, @@ -167,18 +168,6 @@ public: */ MySQL(const string _path, const string _username, const string _password, const string _db, const uint32_t _available = 1, const time_loop_type _engine_type = time_loop_type::internal); - - /** - * Define the maximum number of attempts to - * reconnect to the server - */ - void set_connect_trys(const uint32_t _trys); - - /** - * Set callback on error - */ - void set_on_error(function _on_error); - /** * Execute the SQL statement */ diff --git a/src/mysql.cpp b/src/mysql.cpp index 7883909..517ecf5 100644 --- a/src/mysql.cpp +++ b/src/mysql.cpp @@ -21,6 +21,8 @@ marcelb::mysql::MySQL::MySQL(const string _path, const string _username, const s return; }); } + // set on initialization to avoid the error + last_loop_time = time(nullptr); } @@ -94,11 +96,16 @@ bool marcelb::mysql::MySQL::disconnect_connection(Connection* connection) { } void marcelb::mysql::MySQL::_tloop() { - for (size_t i = 0; i < pool_size && run_tloop; i++) { + if (!run_tloop) { + return; + } + lock_guard lock(io); + for (size_t i=0; iisValid()) { - release_connection(conn); + connection_pool.push(conn); } else { if (!conn->isClosed()){ conn->close(); @@ -112,21 +119,15 @@ void marcelb::mysql::MySQL::_tloop() { } } } - last_loop_time = time(nullptr); -} -void marcelb::mysql::MySQL::set_connect_trys(const uint32_t _trys) { - // lock_guard lock(io); - connect_trys = _trys; -} - -void marcelb::mysql::MySQL::set_on_error(function _on_error) { - on_error = _on_error; + last_loop_time = time(nullptr); } Connection* marcelb::mysql::MySQL::occupy_connection() { - if (last_loop_time + (MYSQL_PERIODIC_INTERNAL_TIME/1000)*3 < time(nullptr) && on_error) { // ako je zadnje vrijeme + 3 intervala manje od trenutnog vremena emitiraj grešku - on_error("The time loop is not executing properly"); + if (last_loop_time + (MYSQL_PERIODIC_INTERNAL_TIME*3/1000) < time(nullptr)) { + if (on_error) { + on_error("The time loop is not executing properly"); + } } unique_lock lock(io); while (connection_pool.empty()) { diff --git a/test/test.cpp b/test/test.cpp index 5ebb1bd..e54990f 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -13,19 +13,20 @@ using namespace marcelb::asynco; int main() { try { - MySQL mydb("tcp://192.168.2.10:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", 5, time_loop_type::internal); - // MySQL mydb("tcp://bitelex.ddns.net:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", 10, time_loop_type::external); + // MySQL mydb("tcp://192.168.2.10:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", 5, time_loop_type::internal); + MySQL mydb("tcp://bitelex.ddns.net:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", 5, time_loop_type::external); // MySQL mydb("tcp://bitelex.ddns.net:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", 5); - mydb.set_on_error( [](const string& error) { + mydb.on_error = [](const string& error) { cout << error << endl; - }); + }; periodic mysql_tloop ( [&mydb] () { + cout << "loop---------------------------" << endl; mydb.tloop(); }, MYSQL_PERIODIC_INTERNAL_TIME); - +while (true) { sleep(5); auto start = high_resolution_clock::now(); @@ -44,8 +45,8 @@ int main() { cout << column_name << endl; } - } catch (const string err) { - cout << err << endl; + } catch (const SQLException error) { + cout << error.what() << endl; } }); @@ -63,8 +64,8 @@ int main() { cout << column_name << endl; } - } catch (const string err) { - cout << err << endl; + } catch (const SQLException error) { + cout << error.what() << endl; } }); @@ -82,8 +83,8 @@ int main() { cout << column_name << endl; } - } catch (const string err) { - cout << err << endl; + } catch (const SQLException error) { + cout << error.what() << endl; } }); @@ -101,8 +102,8 @@ int main() { cout << column_name << endl; } - } catch (const string err) { - cout << err << endl; + } catch (const SQLException error) { + cout << error.what() << endl; } }); @@ -120,8 +121,8 @@ int main() { cout << column_name << endl; } - } catch (const string err) { - cout << err << endl; + } catch (const SQLException error) { + cout << error.what() << endl; } }); @@ -139,8 +140,8 @@ int main() { cout << column_name << endl; } - } catch (const string err) { - cout << err << endl; + } catch (const SQLException error) { + cout << error.what() << endl; } }); @@ -156,9 +157,10 @@ int main() { auto duration = duration_cast(end - start); cout << "-------------Izvršilo se za: " << (double)(duration.count() / 1000.0) << " ms"<< endl; - sleep(100); - +} + sleep(100); + } catch (const SQLException error) { cout << error.what() << endl; } catch (const string error) {