Try fix connection disconnect by server

queue v0.6_fix1
marcelb 11 months ago
parent 81f67c1420
commit d4d0c3155d
  1. 22
      src/mysql.cpp
  2. 39
      test/test.cpp
  3. BIN
      test/test.o

@ -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,9 +123,17 @@ 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; i<con.size() && runBot; i++) {
if (!con[i]->isValid()) {
cout << "Connection is not valid, remove it from pool!" << endl;
io.lock();
con.erase(con.begin()+i);
io.unlock();
i--;
}
}
}
return;
});
@ -297,19 +306,18 @@ void mySQL::getColumns(const string _table, vector<string> &_columns, Connection
Connection* mySQL::shift_con() {
while (true) {
while(con.size()) {
io.lock();
for (int i=0; i<con.size(); i++) {
Connection* con_ptr = con[0];
con.pop_front();
if (con_ptr->isValid()) {
io.unlock();
return con_ptr;
} else {
--i;
}
}
io.unlock();
}
usleep(1000);
}
}
mySQL::~mySQL() {

@ -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;
}

Binary file not shown.
Loading…
Cancel
Save