diff --git a/src/mysql.cpp b/src/mysql.cpp index f46b453..0a3a1fb 100644 --- a/src/mysql.cpp +++ b/src/mysql.cpp @@ -103,8 +103,9 @@ mySQL::mySQL(const string _path, const string _username, const string _password, drv = get_mysql_driver_instance(); - bot = async(launch::async, [&]{ + bot = async(launch::async, [&](){ while (runBot) { + sleep(1); while (available>con.size() && runBot) { try { Connection* new_con_ptr = create_con(); @@ -122,11 +123,19 @@ mySQL::mySQL(const string _path, const string _username, const string _password, cout << except << endl; } } - usleep(1000); // za smanjenje zauzeća procesora - } + for (int i=0; iisValid()) { + cout << "Connection is not valid, remove it from pool!" << endl; + io.lock(); + con.erase(con.begin()+i); + io.unlock(); + i--; + } + } + } return; - }); + }); } @@ -297,18 +306,17 @@ void mySQL::getColumns(const string _table, vector &_columns, Connection Connection* mySQL::shift_con() { while (true) { - io.lock(); - for (int i=0; iisValid()) { io.unlock(); return con_ptr; - } else { - --i; - } + } + io.unlock(); } - io.unlock(); + usleep(1000); } } diff --git a/test/test.cpp b/test/test.cpp index 38ee9c7..fb69dfe 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -13,7 +13,7 @@ int main() { mySQL mydb("tcp://192.168.2.10:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", 5); // sleep(3600*10); - sleep(30); + sleep(20); auto start = high_resolution_clock::now(); @@ -99,24 +99,27 @@ int main() { // t6.join(); // one by one - // try { - // sqlQA test_qa; - // test_qa.select().from("records").where("enabled = 1"); - // mydb.exec(test_qa); - // test_qa.print(true); - // } catch (const string err) { - // cout << err << endl; - // } + try { + sqlQA test_qa; + test_qa.select().from("records").where("enabled = 1"); + mydb.exec(test_qa); + test_qa.print(true); + } catch (const string err) { + cout << err << endl; + } + sleep(20); - // try { - // sqlQA test_qa; - // test_qa.select().from("users"); - // mydb.exec(test_qa); - // test_qa.print(true); - // } catch (const string err) { - // cout << err << endl; - // } + try { + sqlQA test_qa; + test_qa.select().from("users"); + mydb.exec(test_qa); + test_qa.print(true); + } catch (const string err) { + cout << err << endl; + } + + sleep(20); try { sqlQA test_qa; @@ -143,7 +146,7 @@ int main() { cout << "Jebi ga" << endl; } - + sleep(600); return 0; } \ No newline at end of file diff --git a/test/test.o b/test/test.o index c324384..83b1b1d 100755 Binary files a/test/test.o and b/test/test.o differ