diff --git a/lib/mysql.hpp b/lib/mysql.hpp index 0c5bf46..7c284ec 100644 --- a/lib/mysql.hpp +++ b/lib/mysql.hpp @@ -9,6 +9,7 @@ #include #include #include "ctime" +#include #include #include @@ -19,7 +20,7 @@ #include #define unlimited 0 -#define reconnectSleep 10000 // in us +#define reconnectSleep 1000 // in us using namespace std; using namespace sql; @@ -31,7 +32,7 @@ namespace mysql { /** * */ -#define MYSQL_PERIODIC_INTERNAL_TIME 1000 +#define MYSQL_PERIODIC_INTERNAL_TIME 5000 /** * An enumeration of how periodic functions will be run @@ -205,13 +206,11 @@ public: stmt->close(); delete stmt; release_connection(connection); - } catch (sql::SQLException& e) { throw runtime_error(e.what()); // std::cerr << "SQLState: " << e.getSQLState() << std::endl; // std::cerr << "Error code: " << e.getErrorCode() << std::endl; } - return result; } diff --git a/src/mysql.cpp b/src/mysql.cpp index 517ecf5..1850603 100644 --- a/src/mysql.cpp +++ b/src/mysql.cpp @@ -1,6 +1,5 @@ #include "../lib/mysql.hpp" - marcelb::mysql::MySQL::MySQL(const string _path, const string _username, const string _password, const string _db, const uint32_t _available, const time_loop_type _engine_type) { path = _path; username = _username; @@ -30,7 +29,6 @@ Connection* marcelb::mysql::MySQL::create_connection() { uint32_t trys = 0; bool status = true; Connection* new_con = NULL; - while (connect_trys == unlimited ? status : (trys <= connect_trys && status)) { try { Connection* con_can = drv->connect(path, username, password); @@ -99,19 +97,33 @@ void marcelb::mysql::MySQL::_tloop() { if (!run_tloop) { return; } - lock_guard lock(io); for (size_t i=0; i lock(io); + conn = connection_pool.front(); + connection_pool.pop(); + } if (conn->isValid()) { + cout << "Validno----" << endl; connection_pool.push(conn); + condition.notify_one(); } else { + cout << "Nije validno----" << endl; if (!conn->isClosed()){ + cout << "Zatvori----" << endl; + conn->close(); } Connection *n_conn = create_connection(); - release_connection(n_conn); + + { + lock_guard lock(io); + connection_pool.push(n_conn); + condition.notify_one(); + } + } } catch (const SQLException &error) { if (on_error) { diff --git a/test/test.cpp b/test/test.cpp index e54990f..524148f 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -12,22 +12,30 @@ using namespace marcelb::mysql; using namespace marcelb::asynco; int main() { + auto inis = rtime_ms(); 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", 5, time_loop_type::external); + MySQL mydb("tcp://bitelex.ddns.net:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", 12, time_loop_type::external); // MySQL mydb("tcp://bitelex.ddns.net:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", 5); + cout << "init: " << rtime_ms() - inis << endl; + mydb.on_error = [](const string& error) { cout << error << endl; }; periodic mysql_tloop ( [&mydb] () { - cout << "loop---------------------------" << endl; - mydb.tloop(); - }, MYSQL_PERIODIC_INTERNAL_TIME); + try { + auto start = rtime_ms(); + mydb.tloop(); + cout << "loop--------------------------- nema error, trajalo: " << rtime_ms() - start << endl; + } catch (...) { + cout << "Bude neki error u loopu" << endl; + } + }, 2000); while (true) { - sleep(5); + sleep(60); auto start = high_resolution_clock::now();