Pool class, and work on mySQL class pool support

queue
marcelb 1 year ago
parent d7dc97cc30
commit 54b1513d7b
  1. 3
      .vscode/settings.json
  2. 44
      lib/mysql.hpp
  3. 165
      src/mysql.cpp

@ -3,6 +3,7 @@
"string": "cpp", "string": "cpp",
"vector": "cpp", "vector": "cpp",
"deque": "cpp", "deque": "cpp",
"ostream": "cpp" "ostream": "cpp",
"future": "cpp"
} }
} }

@ -7,6 +7,7 @@
#include <map> #include <map>
#include <iostream> #include <iostream>
#include <mutex> #include <mutex>
#include <thread>
#include <mysql_driver.h> #include <mysql_driver.h>
#include <mysql_connection.h> #include <mysql_connection.h>
@ -16,7 +17,8 @@
#include <cppconn/prepared_statement.h> #include <cppconn/prepared_statement.h>
#include <cppconn/resultset.h> #include <cppconn/resultset.h>
#define CONNECT_TRY_LIMIT 3 #define unlimited 0
#define reconnectSleep 100000 // in us
using namespace std; using namespace std;
using namespace sql; using namespace sql;
@ -59,18 +61,50 @@ class mySQL {
public: public:
mutex io; mutex io;
MySQL_Driver *drv; MySQL_Driver *drv;
Connection *con; // Connection *con;
vector<Connection*> con;
string path, username, password, db; string path, username, password, db;
bool isPersistent; bool isPersistent;
uint numOfCon;
uint reconTrys = 3;
mySQL(const string _path, const string _username, const string _password, const string _db, bool _isPersistent = false); mySQL(const string _path, const string _username, const string _password, const string _db, const bool _isPersistent = false, const uint _numOfCon = 1);
bool open(const string _db = ""); bool open(const string _db = "", const int con_idx = -1);
bool connect(); bool connect(const int con_idx = -1);
bool disconnect(); bool disconnect();
void reconnectTrys(const uint _trys);
void exec(sqlQA &sql_qa); void exec(sqlQA &sql_qa);
void getColumns(const string _table, vector<string> &_columns); void getColumns(const string _table, vector<string> &_columns);
~mySQL(); ~mySQL();
}; };
// class mySQLPool {
// public:
// struct Drop {
// mySQL* instance;
// bool used = false;
// };
// struct Swimmer {
// thread instance;
// bool used = false;
// };
// mutex io;
// uint maxpools = 0;
// vector<struct Drop> droplets;
// bool fixServer = false;
// bool fixScheme = false;
// vector<struct Swimmer> swimmers;
// mySQLPool(const uint _maxpools);
// mySQLPool(const uint _maxpools, const string _path, const string _username, const string _password, const string _db);
// void exec(sqlQA &sql_qa, const string _db = "");
// };
#endif #endif

@ -74,12 +74,15 @@ void sqlQA::parse_columns(const string _columns) {
} }
} }
mySQL::mySQL(const string _path, const string _username, const string _password, const string _db, bool _isPersistent) { mySQL::mySQL(const string _path, const string _username, const string _password, const string _db, const bool _isPersistent, const uint _numOfCon) {
path = _path; path = _path;
username = _username; username = _username;
password = _password; password = _password;
db = _db; db = _db;
isPersistent = _isPersistent; isPersistent = _numOfCon > 1 ? true : _isPersistent;
numOfCon = _numOfCon;
drv = get_mysql_driver_instance();
if (isPersistent) { if (isPersistent) {
if (connect()) { if (connect()) {
@ -95,39 +98,52 @@ mySQL::mySQL(const string _path, const string _username, const string _password,
} }
bool mySQL::open(const string _db) { bool mySQL::open(const string _db, const int con_idx) {
io.lock(); io.lock();
db = _db.empty() ? db : _db; db = _db.empty() ? db : _db;
bool status = true; bool status = true; // ako true greška je
try { for (uint i=0; i<con.size(); i++) {
con->setSchema(db); con_idx != -1 ? i = con_idx : i;
status = false; try {
io.unlock(); con[i]->setSchema(db);
} status = false;
catch (const SQLException &error) { io.unlock();
cout << error.what() << endl; }
io.unlock(); catch (const SQLException &error) {
cout << error.what() << endl;
io.unlock();
}
con_idx != -1 ? i = con.size() : i;
} }
return status; return status;
} }
bool mySQL::connect() { bool mySQL::connect(const int con_idx) {
io.lock(); io.lock();
uint trys = 0; uint trys = 0;
bool status = true; bool status = true;
while (trys < CONNECT_TRY_LIMIT && status) { for (uint i=0; i<numOfCon; i++) {
try { con_idx != -1 ? i = con_idx : i;
drv = get_mysql_driver_instance(); while (reconTrys == 0 ? status : (trys < reconTrys && status)) {
con = drv->connect(path, username, password); try {
status = false; if (con_idx == -1) {
io.unlock(); con.push_back(drv->connect(path, username, password));
} }
catch (const SQLException &error) { else {
cout << error.what() << endl; con[i] = drv->connect(path, username, password);
usleep(10000*trys++); }
io.unlock(); status = false;
io.unlock();
}
catch (const SQLException &error) {
cout << error.what() << endl;
usleep(reconnectSleep);
reconTrys == 0 ? trys : trys++;
io.unlock();
}
} }
} }
@ -138,38 +154,54 @@ bool mySQL::disconnect() {
io.lock(); io.lock();
bool status = true; bool status = true;
if (con->isValid() && !con->isClosed()) { for (uint i=0; i<con.size(); i++) {
try { if (con[i]->isValid() && !con[i]->isClosed()) {
con->close(); try {
status = false; con[i]->close();
io.unlock(); status = false;
} io.unlock();
catch (const SQLException &error) { }
cout << error.what() << endl; catch (const SQLException &error) {
status = true; cout << error.what() << endl;
io.unlock(); status = true;
io.unlock();
}
} }
}
else { else {
status = false; status = false; // već je zatvorena
}
} }
return status; return status;
} }
/**
* Broj pokušaja usljed povezivanja s bazom od 1 do unlimited;
*/
void mySQL::reconnectTrys(const uint _trys) {
io.lock();
reconTrys = _trys;
io.unlock();
}
void mySQL::exec(sqlQA &sql_qa) { void mySQL::exec(sqlQA &sql_qa) {
if (!isPersistent || !con->isValid() || con->isClosed()) { for (uint i=0; i<con.size(); i++) {
if (connect()) { if (!isPersistent || !con[i]->isValid() || con[i]->isClosed()) {
throw string("[ERROR] Unable to connect database "); if (connect(i)) {
} throw string("[ERROR] Unable to connect database ");
}
if (open()) { if (open(i)) {
throw string("[ERROR] Unable to open database " + db); throw string("[ERROR] Unable to open database " + db);
}
} }
} }
// find free connection
io.lock(); io.lock();
/**/ /**/
try { try {
@ -249,3 +281,50 @@ mySQL::~mySQL() {
throw string("[ERROR] Unable to close database "); throw string("[ERROR] Unable to close database ");
} }
} }
// mySQLPool::mySQLPool(const uint _maxpools) {
// maxpools = _maxpools;
// fixServer = false;
// fixScheme = true;
// }
// mySQLPool::mySQLPool(const uint _maxpools, const string _path, const string _username, const string _password, const string _db) {
// maxpools = _maxpools;
// fixServer = true;
// fixScheme = !_db.empty();
// for (uint i=0; i<maxpools; i++) {
// mySQL tmpmysql(_path, _username, _password, _db);
// // struct Drop tmpdrop(new mySQL(_path, _username, _password, _db), false);
// droplets.push_back({mySQL(_path, _username, _password, _db, true), false});
// }
// }
// void mySQLPool::exec(sqlQA &sql_qa, const string _db) {
// if (!fixScheme && _db.empty()) {
// throw string("[ERROR] Database is not selected! ");
// }
// for (uint i=0; i<droplets.size(); i++) {
// if (droplets[i].used == false) {
// droplets[i].used = true;
// bool isRunned = false;
// while (!isRunned) {
// for (uint i=0; i<swimmers.size(); i++) {
// if (swimmers[i].used == false) {
// swimmers[i].used = true;
// swimmers[i].instance = thread([&]() {
// droplets[i].instance->exec(sql_qa);
// });
// swimmers[i].instance.join();
// swimmers[i].used = false;
// isRunned = true;
// }
// }
// }
// droplets[i].used = false;
// }
// }
// }

Loading…
Cancel
Save