Fix connection close, and implement bot for reconnect

queue v0.4_pool
marcelb 10 months ago
parent 6905bfe745
commit 774151e534
  1. 3
      lib/mysql.hpp
  2. 62
      src/mysql.cpp
  3. 83
      test/test.cpp
  4. BIN
      test/test.o

@ -8,6 +8,7 @@
#include <iostream> #include <iostream>
#include <mutex> #include <mutex>
#include <thread> #include <thread>
#include <future>
#include <mysql_driver.h> #include <mysql_driver.h>
#include <mysql_connection.h> #include <mysql_connection.h>
@ -68,6 +69,8 @@ class mySQL {
bool isPersistent; bool isPersistent;
uint numOfCon; uint numOfCon;
uint reconTrys = 3; uint reconTrys = 3;
bool runBot = true;
future<void> bot;
void getColumns(const string _table, vector<string> &_columns, Connection *ptr_con); // privatno void getColumns(const string _table, vector<string> &_columns, Connection *ptr_con); // privatno
bool open_one(const uint idx); bool open_one(const uint idx);

@ -114,6 +114,34 @@ mySQL::mySQL(const string _path, const string _username, const string _password,
throw string("[ERROR] Unable to open database " + db); throw string("[ERROR] Unable to open database " + db);
} }
} }
bot = async(launch::async, [&]{
while (runBot) {
try {
for (uint i=0; i<con.size(); i++) {
if (con[i].first->try_lock()) {
if (!con[i].second->isValid()) {
if (connect_one(i)) {
throw string("[ERROR] Unable to connect database ");
}
if (!db.empty()) {
if (open_one(i)) {
throw string("[ERROR] Unable to open database " + db);
}
}
}
con[i].first->unlock();
}
}
} catch (const SQLException except) {
cout << except.what() << endl;
} catch (const string except) {
cout << except << endl;
}
}
return;
});
} }
} }
@ -184,6 +212,7 @@ bool mySQL::disconnect_one(const uint idx) {
status = false; // već je zatvorena status = false; // već je zatvorena
} }
delete con[idx].second;
return status; return status;
} }
@ -236,9 +265,36 @@ void mySQL::reconnectTrys(const uint _trys) {
} }
void mySQL::exec(sqlQA &sql_qa) { void mySQL::exec(sqlQA &sql_qa) {
if (!isPersistent) {
if (connect()) {
throw string("[ERROR] Unable to connect database ");
}
if (!db.empty()) {
if (open()) {
throw string("[ERROR] Unable to open database " + db);
}
}
}
const uint idx = findFreeCon(); const uint idx = findFreeCon();
if (!isPersistent || !con[idx].second->isValid() || con[idx].second->isClosed()) { if (!isPersistent || con[idx].second->isClosed()) {
if (connect_one(idx)) {
throw string("[ERROR] Unable to connect database ");
}
if (open_one(idx)) {
throw string("[ERROR] Unable to open database " + db);
}
}
else if (!con[idx].second->isValid()) {
if(disconnect_one(idx)) {
throw string("[ERROR] Unable to close database ");
}
if (connect_one(idx)) { if (connect_one(idx)) {
throw string("[ERROR] Unable to connect database "); throw string("[ERROR] Unable to connect database ");
} }
@ -325,14 +381,18 @@ uint mySQL::findFreeCon() {
lock_guard<mutex> master(io); lock_guard<mutex> master(io);
while (true) { while (true) {
for (uint i=0; i<con.size(); i++) { for (uint i=0; i<con.size(); i++) {
// if (con[i].second->isValid()) {
if (con[i].first->try_lock()) { if (con[i].first->try_lock()) {
return i; return i;
} }
// }
} }
} }
} }
mySQL::~mySQL() { mySQL::~mySQL() {
runBot = false;
bot.get();
if(disconnect()) { if(disconnect()) {
throw string("[ERROR] Unable to close database "); throw string("[ERROR] Unable to close database ");
} }

@ -18,38 +18,73 @@ int main() {
sqlQA test_qa; sqlQA test_qa;
test_qa.select().from("records").where("enabled = 1"); test_qa.select().from("records").where("enabled = 1");
mydb.exec(test_qa); mydb.exec(test_qa);
test_qa.print(true); //test_qa.print(true);
} catch (const string err) { } catch (const string err) {
cout << err << endl; cout << err << endl;
} }
}); });
thread t2([&](){ // thread t2([&](){
try { // try {
sqlQA test_qa; // sqlQA test_qa;
test_qa.select().from("zones"); // test_qa.select().from("zones");
mydb.exec(test_qa); // mydb.exec(test_qa);
test_qa.print(true); // //test_qa.print(true);
} catch (const string err) { // } catch (const string err) {
cout << err << endl; // cout << err << endl;
} // }
}); // });
thread t3([&](){ // thread t3([&](){
try { // try {
sqlQA test_qa; // sqlQA test_qa;
test_qa.select().from("users"); // test_qa.select().from("users");
mydb.exec(test_qa); // mydb.exec(test_qa);
test_qa.print(true); // //test_qa.print(true);
} catch (const string err) { // } catch (const string err) {
cout << err << endl; // cout << err << endl;
} // }
}); // });
// thread t4([&](){
// 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;
// }
// });
// thread t5([&](){
// try {
// sqlQA test_qa;
// test_qa.select().from("zones");
// mydb.exec(test_qa);
// //test_qa.print(true);
// } catch (const string err) {
// cout << err << endl;
// }
// });
// thread t6([&](){
// try {
// sqlQA test_qa;
// test_qa.select().from("users");
// mydb.exec(test_qa);
// // test_qa.print(true);
// } catch (const string err) {
// cout << err << endl;
// }
// });
t1.join(); t1.join();
t2.join(); // t2.join();
t3.join(); // t3.join();
// t4.join();
// t5.join();
// t6.join();
// one by one // one by one
// try { // try {
@ -94,6 +129,8 @@ int main() {
cout << "Jebi ga" << endl; cout << "Jebi ga" << endl;
} }
sleep(100);
return 0; return 0;
} }

Binary file not shown.
Loading…
Cancel
Save