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. 24
      src/mysql.cpp
  3. 18
      test/test.cpp

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

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

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

Loading…
Cancel
Save