Pool class
This commit is contained in:
parent
d7dc97cc30
commit
c5f051b782
4
.vscode/settings.json
vendored
4
.vscode/settings.json
vendored
@ -3,6 +3,8 @@
|
|||||||
"string": "cpp",
|
"string": "cpp",
|
||||||
"vector": "cpp",
|
"vector": "cpp",
|
||||||
"deque": "cpp",
|
"deque": "cpp",
|
||||||
"ostream": "cpp"
|
"ostream": "cpp",
|
||||||
|
"iostream": "cpp",
|
||||||
|
"ratio": "cpp"
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -73,4 +73,15 @@ class mySQL {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
class mySQLPool {
|
||||||
|
public:
|
||||||
|
uint numOfCon = 1;
|
||||||
|
vector<mySQL*> drops;
|
||||||
|
vector<bool> dropsdown; // true is used
|
||||||
|
|
||||||
|
mySQLPool(const uint _numOfCon, const string _path, const string _username, const string _password, const string _db);
|
||||||
|
void exec(sqlQA &sql_qa);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -117,6 +117,8 @@ bool mySQL::connect() {
|
|||||||
uint trys = 0;
|
uint trys = 0;
|
||||||
bool status = true;
|
bool status = true;
|
||||||
|
|
||||||
|
cout << "-----------Jebiga connect " << endl;
|
||||||
|
|
||||||
while (trys < CONNECT_TRY_LIMIT && status) {
|
while (trys < CONNECT_TRY_LIMIT && status) {
|
||||||
try {
|
try {
|
||||||
drv = get_mysql_driver_instance();
|
drv = get_mysql_driver_instance();
|
||||||
@ -161,6 +163,7 @@ bool mySQL::disconnect() {
|
|||||||
void mySQL::exec(sqlQA &sql_qa) {
|
void mySQL::exec(sqlQA &sql_qa) {
|
||||||
|
|
||||||
if (!isPersistent || !con->isValid() || con->isClosed()) {
|
if (!isPersistent || !con->isValid() || con->isClosed()) {
|
||||||
|
cout << "--------Đubre nije spojeno!!" << con->isValid() << con->isClosed() << endl;
|
||||||
if (connect()) {
|
if (connect()) {
|
||||||
throw string("[ERROR] Unable to connect database ");
|
throw string("[ERROR] Unable to connect database ");
|
||||||
}
|
}
|
||||||
@ -223,6 +226,7 @@ void mySQL::exec(sqlQA &sql_qa) {
|
|||||||
/**/
|
/**/
|
||||||
|
|
||||||
if (!isPersistent) {
|
if (!isPersistent) {
|
||||||
|
cout << "---------Gasi!" << endl;
|
||||||
if(disconnect()) {
|
if(disconnect()) {
|
||||||
throw string("[ERROR] Unable to close database ");
|
throw string("[ERROR] Unable to close database ");
|
||||||
}
|
}
|
||||||
@ -249,3 +253,35 @@ mySQL::~mySQL() {
|
|||||||
throw string("[ERROR] Unable to close database ");
|
throw string("[ERROR] Unable to close database ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
mySQLPool::mySQLPool(const uint _numOfCon, const string _path, const string _username, const string _password, const string _db) {
|
||||||
|
if (_numOfCon > 0) {
|
||||||
|
numOfCon = _numOfCon;
|
||||||
|
} else {
|
||||||
|
throw string("[ERROR] The number of MySQL connections must be greater than zero ");
|
||||||
|
}
|
||||||
|
|
||||||
|
drops.resize(numOfCon);
|
||||||
|
dropsdown.resize(numOfCon);
|
||||||
|
|
||||||
|
for (uint i=0; i<numOfCon; i++) {
|
||||||
|
drops[i] = new mySQL(_path, _username, _password, _db, true);
|
||||||
|
// dropsdown.push_back(new mutex());
|
||||||
|
dropsdown[i] = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void mySQLPool::exec(sqlQA &sql_qa) {
|
||||||
|
for (uint i=0; i<drops.size(); i++) {
|
||||||
|
if (!dropsdown[i]) {
|
||||||
|
cout << "--------Run dorp "<< i << endl;
|
||||||
|
dropsdown[i] = true;
|
||||||
|
usleep(10000);
|
||||||
|
drops[i]->exec(sql_qa);
|
||||||
|
dropsdown[i] = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,5 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
#include "../lib/mysql.hpp"
|
#include "../lib/mysql.hpp"
|
||||||
|
|
||||||
@ -6,14 +7,23 @@ using namespace std;
|
|||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
||||||
mySQL mydb("tcp://192.168.2.10:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", true);
|
// mySQL mydb("tcp://192.168.2.10:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", true);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
sqlQA test_qa;
|
mySQLPool bazenbaze(5, "tcp://192.168.2.10:3306", "dinio", "H€r5elfInd1aH@nds", "dinio");
|
||||||
// id,user_id,zone_id,domain,record_type,auth_key,last_update,enabled
|
|
||||||
// test_qa.select().from("records").where("enabled = 0").limit(2);
|
// sqlQA test_qa;
|
||||||
// mydb.exec(test_qa);
|
// // id,user_id,zone_id,domain,record_type,auth_key,last_update,enabled
|
||||||
|
// test_qa.select().from("users");
|
||||||
|
// // mydb.exec(test_qa);
|
||||||
|
// bazenbaze.exec(test_qa);
|
||||||
|
|
||||||
|
// sqlQA test_qa2;
|
||||||
|
// // id,user_id,zone_id,domain,record_type,auth_key,last_update,enabled
|
||||||
|
// test_qa2.select().from("records").where("enabled = 1");
|
||||||
|
// // mydb.exec(test_qa2);
|
||||||
|
// bazenbaze.exec(test_qa2);
|
||||||
|
|
||||||
// for (auto i : test_qa.result) {
|
// for (auto i : test_qa.result) {
|
||||||
// for (auto j: i.second) {
|
// for (auto j: i.second) {
|
||||||
@ -21,6 +31,68 @@ int main() {
|
|||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
// for (auto i : test_qa2.result) {
|
||||||
|
// for (auto j: i.second) {
|
||||||
|
// cout << i.first << " : " << j << endl;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// sleep(600);
|
||||||
|
|
||||||
|
|
||||||
|
thread t1([&](){
|
||||||
|
sqlQA test_qa;
|
||||||
|
// id,user_id,zone_id,domain,record_type,auth_key,last_update,enabled
|
||||||
|
test_qa.select().from("users");
|
||||||
|
// mydb.exec(test_qa);
|
||||||
|
bazenbaze.exec(test_qa);
|
||||||
|
|
||||||
|
for (auto i : test_qa.result) {
|
||||||
|
for (auto j: i.second) {
|
||||||
|
cout << i.first << " : " << j << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
thread t2([&](){
|
||||||
|
sqlQA test_qa2;
|
||||||
|
// id,user_id,zone_id,domain,record_type,auth_key,last_update,enabled
|
||||||
|
test_qa2.select().from("records").where("enabled = 1");
|
||||||
|
// mydb.exec(test_qa2);
|
||||||
|
bazenbaze.exec(test_qa2);
|
||||||
|
|
||||||
|
for (auto i : test_qa2.result) {
|
||||||
|
for (auto j: i.second) {
|
||||||
|
cout << i.first << " : " << j << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
thread t3([&](){
|
||||||
|
sqlQA test_qa3;
|
||||||
|
// id,user_id,zone_id,domain,record_type,auth_key,last_update,enabled
|
||||||
|
test_qa3.select().from("zones").where("enabled = 1");
|
||||||
|
// mydb.exec(test_qa3);
|
||||||
|
bazenbaze.exec(test_qa3);
|
||||||
|
|
||||||
|
for (auto i : test_qa3.result) {
|
||||||
|
for (auto j: i.second) {
|
||||||
|
cout << i.first << " : " << j << endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
t1.join();
|
||||||
|
t2.join();
|
||||||
|
t3.join();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// sleep(600);
|
||||||
|
|
||||||
|
|
||||||
// test_qa.update("records").set("enabled = 1").where("domain = 'bitelex.test'");
|
// test_qa.update("records").set("enabled = 1").where("domain = 'bitelex.test'");
|
||||||
// mydb.exec(test_qa);
|
// mydb.exec(test_qa);
|
||||||
@ -32,11 +104,11 @@ int main() {
|
|||||||
|
|
||||||
|
|
||||||
// test_qa.insertInTo("records", "id,user_id,zone_id,domain,record_type,auth_key,last_update,enabled").values("'5',2,2,'www.bitelex.test','AAAA','jebiga',NULL,1");
|
// test_qa.insertInTo("records", "id,user_id,zone_id,domain,record_type,auth_key,last_update,enabled").values("'5',2,2,'www.bitelex.test','AAAA','jebiga',NULL,1");
|
||||||
test_qa.deleteFrom("records").where("record_type = AAAA");
|
// test_qa.deleteFrom("records").where("record_type = AAAA");
|
||||||
// test_qa.update("records").set("enabled = 0").where("record_type = 'AAAA'");
|
// // test_qa.update("records").set("enabled = 0").where("record_type = 'AAAA'");
|
||||||
cout << test_qa.cmd << endl;
|
// cout << test_qa.cmd << endl;
|
||||||
mydb.exec(test_qa);
|
// mydb.exec(test_qa);
|
||||||
cout << "Num rows " << test_qa.num_rows << " num columns " << test_qa.num_columns << " catch " << test_qa.updateCatch << " executed " << test_qa.executed << endl;
|
// cout << "Num rows " << test_qa.num_rows << " num columns " << test_qa.num_columns << " catch " << test_qa.updateCatch << " executed " << test_qa.executed << endl;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
BIN
test/test.o
BIN
test/test.o
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user