isValid to long, limited time in periodic is problem!

fix-time-limit
mbandic 2 months ago
parent b04cf84eaa
commit c84fcf5bf6
  1. 7
      lib/mysql.hpp
  2. 22
      src/mysql.cpp
  3. 16
      test/test.cpp

@ -9,6 +9,7 @@
#include <vector>
#include <tuple>
#include "ctime"
#include <chrono>
#include <mysql_driver.h>
#include <mysql_connection.h>
@ -19,7 +20,7 @@
#include <cppconn/resultset.h>
#define unlimited 0
#define reconnectSleep 10000 // in us
#define reconnectSleep 1000 // in us
using namespace std;
using namespace sql;
@ -31,7 +32,7 @@ namespace mysql {
/**
*
*/
#define MYSQL_PERIODIC_INTERNAL_TIME 1000
#define MYSQL_PERIODIC_INTERNAL_TIME 5000
/**
* An enumeration of how periodic functions will be run
@ -205,13 +206,11 @@ public:
stmt->close();
delete stmt;
release_connection(connection);
} catch (sql::SQLException& e) {
throw runtime_error(e.what());
// std::cerr << "SQLState: " << e.getSQLState() << std::endl;
// std::cerr << "Error code: " << e.getErrorCode() << std::endl;
}
return result;
}

@ -1,6 +1,5 @@
#include "../lib/mysql.hpp"
marcelb::mysql::MySQL::MySQL(const string _path, const string _username, const string _password, const string _db, const uint32_t _available, const time_loop_type _engine_type) {
path = _path;
username = _username;
@ -30,7 +29,6 @@ Connection* marcelb::mysql::MySQL::create_connection() {
uint32_t trys = 0;
bool status = true;
Connection* new_con = NULL;
while (connect_trys == unlimited ? status : (trys <= connect_trys && status)) {
try {
Connection* con_can = drv->connect(path, username, password);
@ -99,19 +97,33 @@ void marcelb::mysql::MySQL::_tloop() {
if (!run_tloop) {
return;
}
lock_guard<mutex> lock(io);
for (size_t i=0; i<connection_pool.size(); i++) {
try {
Connection *conn = connection_pool.front();
Connection *conn = nullptr;
{
lock_guard<mutex> lock(io);
conn = connection_pool.front();
connection_pool.pop();
}
if (conn->isValid()) {
cout << "Validno----" << endl;
connection_pool.push(conn);
condition.notify_one();
} else {
cout << "Nije validno----" << endl;
if (!conn->isClosed()){
cout << "Zatvori----" << endl;
conn->close();
}
Connection *n_conn = create_connection();
release_connection(n_conn);
{
lock_guard<mutex> lock(io);
connection_pool.push(n_conn);
condition.notify_one();
}
}
} catch (const SQLException &error) {
if (on_error) {

@ -12,22 +12,30 @@ using namespace marcelb::mysql;
using namespace marcelb::asynco;
int main() {
auto inis = rtime_ms();
try {
// MySQL mydb("tcp://192.168.2.10:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", 5, time_loop_type::internal);
MySQL mydb("tcp://bitelex.ddns.net:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", 5, time_loop_type::external);
MySQL mydb("tcp://bitelex.ddns.net:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", 12, time_loop_type::external);
// MySQL mydb("tcp://bitelex.ddns.net:3306", "dinio", "H€r5elfInd1aH@nds", "dinio", 5);
cout << "init: " << rtime_ms() - inis << endl;
mydb.on_error = [](const string& error) {
cout << error << endl;
};
periodic mysql_tloop ( [&mydb] () {
cout << "loop---------------------------" << endl;
try {
auto start = rtime_ms();
mydb.tloop();
}, MYSQL_PERIODIC_INTERNAL_TIME);
cout << "loop--------------------------- nema error, trajalo: " << rtime_ms() - start << endl;
} catch (...) {
cout << "Bude neki error u loopu" << endl;
}
}, 2000);
while (true) {
sleep(5);
sleep(60);
auto start = high_resolution_clock::now();

Loading…
Cancel
Save