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",
"vector": "cpp",
"deque": "cpp",
"ostream": "cpp"
"ostream": "cpp",
"future": "cpp"
}
}

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

@ -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;
username = _username;
password = _password;
db = _db;
isPersistent = _isPersistent;
isPersistent = _numOfCon > 1 ? true : _isPersistent;
numOfCon = _numOfCon;
drv = get_mysql_driver_instance();
if (isPersistent) {
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();
db = _db.empty() ? db : _db;
bool status = true;
bool status = true; // ako true greška je
try {
con->setSchema(db);
status = false;
io.unlock();
for (uint i=0; i<con.size(); i++) {
con_idx != -1 ? i = con_idx : i;
try {
con[i]->setSchema(db);
status = false;
io.unlock();
}
catch (const SQLException &error) {
cout << error.what() << endl;
io.unlock();
}
con_idx != -1 ? i = con.size() : i;
}
catch (const SQLException &error) {
cout << error.what() << endl;
io.unlock();
}
return status;
}
bool mySQL::connect() {
bool mySQL::connect(const int con_idx) {
io.lock();
uint trys = 0;
bool status = true;
while (trys < CONNECT_TRY_LIMIT && status) {
try {
drv = get_mysql_driver_instance();
con = drv->connect(path, username, password);
status = false;
io.unlock();
}
catch (const SQLException &error) {
cout << error.what() << endl;
usleep(10000*trys++);
io.unlock();
for (uint i=0; i<numOfCon; i++) {
con_idx != -1 ? i = con_idx : i;
while (reconTrys == 0 ? status : (trys < reconTrys && status)) {
try {
if (con_idx == -1) {
con.push_back(drv->connect(path, username, password));
}
else {
con[i] = drv->connect(path, username, password);
}
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();
bool status = true;
if (con->isValid() && !con->isClosed()) {
try {
con->close();
status = false;
io.unlock();
}
catch (const SQLException &error) {
cout << error.what() << endl;
status = true;
io.unlock();
for (uint i=0; i<con.size(); i++) {
if (con[i]->isValid() && !con[i]->isClosed()) {
try {
con[i]->close();
status = false;
io.unlock();
}
catch (const SQLException &error) {
cout << error.what() << endl;
status = true;
io.unlock();
}
}
}
else {
status = false;
else {
status = false; // već je zatvorena
}
}
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) {
if (!isPersistent || !con->isValid() || con->isClosed()) {
if (connect()) {
throw string("[ERROR] Unable to connect database ");
}
for (uint i=0; i<con.size(); i++) {
if (!isPersistent || !con[i]->isValid() || con[i]->isClosed()) {
if (connect(i)) {
throw string("[ERROR] Unable to connect database ");
}
if (open()) {
throw string("[ERROR] Unable to open database " + db);
if (open(i)) {
throw string("[ERROR] Unable to open database " + db);
}
}
}
// find free connection
io.lock();
/**/
try {
@ -249,3 +281,50 @@ mySQL::~mySQL() {
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